I'm making an application with a list page and the detail page(Item.vue) simply. From the list, a user can go to the detail page with the url like localhost/item/12345. But I cannot make the logic for my Laravel application with VueJS and Vue-router.
Laravel 5.4,
node 6.11, npm 3.10.10,
axios 0.15.3, vue 2.3.4, vue-router 2.6.0
My code is like below
Example.vue (using as a list page)
<template>
<p class="card-text"><router-link v-bind:to="{ name:'item', params:{ id: item.id }}">
{{ item.title }}</router-link>
</p>
</template>
<script>
export default {
data(){
return{
$item:[]
}
},
mounted(){
var self = this;
axios.get('/item').then(function(response){
return self.item = response.data;
});
}
}
</script>
Item.vue
<template>
<p class="card-text">{{item.title}}</p>
<p class="card-text">{{item.content}}</p>
</template>
<script>
export default {
data(){
return{
$item:[]
}
},
mounted(){
var self = this;
axios.get('/item'+this.$route.params.slug
).then(function(response){
return self.item = response.data;
});
}
}
</script>
routes.js
import VueRouter from 'vue-router';
var routes=[
{
name:'item',
path:'/item/:id',
component:require('./components/Item.vue'),
props: true
}
];
export default new VueRouter({
routes
});
routes/web.js
Route::resource('item','ItemController');
Route::resource('item/{id}', 'ItemController@show');
ItemController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Items;
class ItemController extends Controller
{
public function index()
{
return Items::all();
}
public function show($id)
{
$item = Items::where('id', '=', $id)->firstOrFail();
return view('item.show', compact('item'));
}
}
app.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import router from './routes.js';
require('./bootstrap');
window.Vue = require('vue');
Vue.use(VueRouter);
Vue.component('example', require('./components/Example.vue'));
const app = new Vue({
router,
el: '#app'
});
the warning message is shown for the list page
[Vue warn]: Property or method "item" is not defined on the instance but referenced
during render. Make sure to declare reactive data properties in the data option.
found in
---> <Item> at /var/www/laravel/resources/assets/js/components/Item.vue
<Root>
the error message for the detail page
Error: Request failed with status code 500
Item.vueplease edit your question and add this file code too