如何将路由参数从Laravel传递到Vue.js
我有一个这样的途径来获取带有相关评论的帖子如何将路由参数从Laravel传递到Vue.js,laravel,laravel-5,vue.js,Laravel,Laravel 5,Vue.js,我有一个这样的途径来获取带有相关评论的帖子 Route::get('/api/topics/{category_id}/{title}', function($category_id, $title){ return App\Topic::with('comments')->where(compact('category_id','title'))->firstOrFail(); }); 问题是如何将参数变量传递给Vue.js?在本例中是“category_id”和“tit
Route::get('/api/topics/{category_id}/{title}', function($category_id, $title){
return App\Topic::with('comments')->where(compact('category_id','title'))->firstOrFail();
});
问题是如何将参数变量传递给Vue.js?在本例中是“category_id”和“title”,这样Vue就可以获取帖子和评论
下面是我的Vue实例,它给出了此错误:
main.js:11749Uncaught ReferenceError: category_id is not defined
Vue实例
new Vue({
el: '#comment',
methods: {
fetchComment: function (category_id, title) {
this.$http.get('/api/topics/' + category_id + '/' + title ,function (data) {
this.$set('topics',data)
})
}
},
ready: function () {
this.fetchComment(category_id, title)
}
});
方法来显示某个帖子
public function show($category_id, $title)
{
$topic = Topic::where(compact('category_id','title'))->firstOrFail();
$comments = Comment::where('topic_id',$topic->id)->get();
return view('forums.category', compact('topic','comments'));
}
ForumsController.php
public function show($category\u id,$title){
$topic=topic::where('category\u id',$category\u id)
->其中('title',$title)
->firstOrFail();
返回视图('forums.category')
->带('topic',$topic);
}
Javascript
var category_id={{{$topic->category_id}};
var title='{{$topic->title}}';
新Vue({
el:'评论',
方法:{
fetchComment:函数(类别id、标题){
此.http.get('/api/topics/'+category_id+'/'+标题,函数(数据){
此.$set('主题',数据);
})
}
},
就绪:函数(){
此.fetchComment(类别id、标题);
}
});
试图通过postlook thismain.js:11749未捕获引用错误:未定义类别id,未定义类别id
并且您在this.fetchComment(类别id,标题)中使用,title not defeinded totry在方法中使用之前声明category\u id&title,例如var category\u id=put\u here\u\u category\u id的\u源代码
,对于title to,我可以看到完整的代码吗?这是否意味着Vue实例无法从上面的api获取数据?有关完整的代码,请查看此部分