尝试显示数据库中的内容时,created(){axios.get方法不起作用

时间:2018-10-25 09:38:59

标签: laravel vue.js vue-component

**我的溃败文件,当我直接在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);
    });
    }
    }});

2 个答案:

答案 0 :(得分:1)

ready不再受支持。这就是Vue v1。您的新方法是mounted。参见editor modehttps://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram

数据也是返回数据对象的函数,因此如果看起来像这样:

data: function() {
   return {
      msg: 'make post',
      content: '',
      posts: []
  }
}

答案 1 :(得分:0)

删除axios网址中的 Future<FirebaseApp> app; void appInit() { app.timeout(Duration(seconds: 5), onTimeout: (){ // handle app timeout here }); app = FirebaseApp.configure( name: 'test', options: const FirebaseOptions( googleAppID: googleAppID, gcmSenderID: projectID, apiKey: apiKey, projectID: projectID, ), ); }

Firestore.instance.runTransaction((Transaction tx) {
  // handle transaction here
}).timeout(Duration(seconds: 5), onTimeout: () {
  // handle transaction timeout here
});

编辑: 尝试删除 this.bUrl功能:

created(){
    axios.get('/posts')
    .then(response => {

您的ready内应有ready: function(){ this.created(); },

data()