Last active
March 7, 2017 02:27
-
-
Save acro5piano/2d644face04fe09bbc766f8ec4d388db to your computer and use it in GitHub Desktop.
LaravelからVueを使ってSPAっぽくする ref: http://qiita.com/acro5piano/items/712b6b84151b53d0c53b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::group(['middleware' => 'api'], function() { | |
Route::get('articles', function() { | |
$articles = Article::all()->take(5); | |
return $articles; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="{{ config('app.locale') }}"> | |
<head> | |
<script> | |
window.Laravel = {}; | |
window.Laravel.csrfToken = "{{ csrf_token() }}"; | |
</script> | |
</head> | |
<body> | |
<div id="app"> | |
<navbar></navbar> | |
<div class="container"> | |
<router-view></router-view> | |
</div> | |
</div> | |
</body> | |
<script src="{{ mix('js/app.js') }}"></script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from 'vue' | |
import VueRouter from 'vue-router' | |
require('./bootstrap') | |
Vue.use(VueRouter) | |
Vue.component('navbar', require('./components/Layouts/Navbar.vue')) | |
const router = new VueRouter({ | |
mode: 'history', | |
routes: [ | |
{ path: '/', | |
component: require('./components/Articles/Index.vue') | |
} | |
] | |
}) | |
const app = new Vue({ | |
router | |
}).$mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
window.axios = require('axios') | |
window.axios.defaults.headers.common = { | |
'X-CSRF-TOKEN': window.Laravel.csrfToken, | |
'X-Requested-With': 'XMLHttpRequest' | |
} | |
Vue.prototype.$http = axios | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
|-- app | |
| `-- Models | |
| `-- Article.php | |
|-- package.json | |
|-- resources | |
| |-- views | |
| | `-- app.blade.php | |
| `-- assets | |
| `-- js | |
| |-- app.js | |
| |-- bootstrap.js | |
| `-- components | |
| |-- Articles | |
| | `-- Index.vue | |
| `-- Layouts | |
| `-- Navbar.vue | |
|-- routes | |
|-- api.php | |
`-- web.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div v-for="article in articles"> | |
<h1> | |
<router-link :to="'/articles/' + article.id">{{ article.title }}</router-link> | |
</h1> | |
<p> | |
{{ article.content }} | |
</p> | |
</div> | |
</template> | |
<script> | |
export default { | |
created() { | |
this.fetchArticles() | |
}, | |
data() { | |
return { | |
articles: [] | |
} | |
}, | |
methods: { | |
fetchArticles() { | |
this.$http({ | |
url: '/api/articles', | |
method: 'GET' | |
}).then(res => { | |
this.articles = res.data | |
}) | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('/{any}', function () { | |
return view('app'); | |
})->where('any', '.*'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment