在创建组件之前,我正在对一些本地json数据进行异步调用.所以这段代码实际上工作正常:

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不是函数(…)

为什么我不能访问beforeCreate钩子中的fetchUsers方法?有什么工作?

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐