Skip to content

Instantly share code, notes, and snippets.

@dayfuaim
Forked from Tekki/mojo-vue.pl
Created Apr 29, 2019
Embed
What would you like to do?
Mojolicious - Vue.js Example
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
get '/' => sub ($c) {
$c->render(template => 'index');
};
get '/api/:region' => sub ($c) {
my %regions = (
north => {
countries => [
'Antigua and Barbuda', 'Bahamas',
'Barbados', 'Belize',
'Canada', 'Costa Rica',
'Cuba', 'Dominica',
'Dominican Republic', 'El Salvador',
'Grenada', 'Guatemala',
'Haiti', 'Honduras',
'Jamaica', 'Mexico',
'Nicaragua', 'Panama',
'Saint Kitts and Nevis', 'Saint Lucia',
'Saint Vincent and the Grenadines', 'Trinidad and Tobago',
'United States',
],
region => 'North America',
},
south => {
countries => [
'Argentina', 'Bolivia', 'Brazil', 'Chile',
'Colombia', 'Ecuador', 'Guyana', 'Paraguay',
'Peru', 'Suriname', 'Uruguay', 'Venezuela',
],
region => 'South America',
},
);
my $region = $regions{$c->param('region')} || {};
$c->render(json => $region);
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Mojolicious-Vue.js Example';
<div id="app">
<h1>Countries in {{ area.region }}</h1>
<table>
<tr v-for="country in area.countries"><td>{{ country }}</td></tr>
</table>
<button v-on:click="showNorth()">Show countries in North America</button>
</div>
%= javascript 'https://unpkg.com/vue/dist/vue.min.js'
%= javascript 'https://unpkg.com/axios/dist/axios.min.js'
%= javascript begin
var app = new Vue({
el: '#app',
created: function() {
this.getRegion('south');
},
data: { area: {} },
methods: {
getRegion: function(area) {
var self = this;
axios.get('/api/' + area).then(
function(response) {
self.area = response.data;
}
);
},
showNorth: function() {
this.getRegion('north');
}
}
});
% end
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment