【发布时间】:2024-04-27 04:40:02
【问题描述】:
**我的 rout 文件,当我直接在 URL 中输入帖子时,它会显示帖子,但在 app.js 中使用创建的方法时,它什么也不显示**
Route::get('/posts', function () {
$posts_json = DB::table('posts')
->orderBy('posts.created_at','desc')->take(4)->get();return $posts_json;}
我的 app.js 文件
const app = new Vue({
el: '#app',
data: {
msg: 'make post',
content:'',
posts: [],
bUrl: 'http://localhost/pathikhome',
},
ready: function(){
this.created();
},
created(){
axios.get(this.bUrl +'/posts')
.then(response => {
console.log(response);
this.posts = response.data;
})
.catch(function (error) {
console.log(error);
});
},
methods: {
addPost(){
axios.post(this.bUrl +'/addPost', {
content:this.content
})
如果不成功
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}});
【问题讨论】:
-
http://localhost/pathikhome是您的应用程序的 URL 吗?还是另一个网址? -
是的。它是应用程序。网址
-
不应该是
created: function() { ... } -
@SurfMan 对不起?
-
不再支持 ready。那是Vue v1。你的新方法是
mounted。见vuejs.org/v2/guide/instance.html#Lifecycle-Diagram 和vuejs.org/v2/guide/migration.html#ready-replaced
标签: laravel vue.js vue-component