7
コンポーネントが作成される前に、ローカルのjson
データに対して非同期呼び出しを行っています。したがって、このコードは実際には正常に動作します:Vue 2.1 beforeCreateフックの呼び出しメソッドが機能していない
beforeCreate : function() {
var self = this;
fetch('/assets/data/radfaces.json')
.then(function(response) { return response.json()
.then(function(data) { self.users = data; });
})
.catch(function(error) {
console.log(error);
});
},
は今、私はちょうどリファクタリングと別の方法にこれを移動したい:
beforeCreate : function() {
this.fetchUsers();
},
methods: {
fetchUsers: function() {
var self = this;
fetch('/assets/data/radfaces.json')
.then(function(response) { return response.json()
.then(function(data) { self.users = data; });
})
.catch(function(error) {
console.log(error);
});
}
}
そして今、すべてが働いて停止します。私はエラーを取得する:app.js:13 Uncaught TypeError: this.fetchUsers is not a function(…)
私はbeforeCreate
フックでfetchUsers
メソッドにアクセスできないのはなぜ?何が回避策ですか?
をドキュメントに、私はこのことについて読むことができる場所を知っています:それは代わりに
created
フックを使用するには、この周りの最も簡単な方法は? –はい、詳しくはこちらをご覧ください:https://vuejs.org/v2/api/#Options-Lifecycle-Hooks –