$ http vueJs未设置 [英] this.$http vueJs not set
问题描述
我正在使用vueJS并尝试从ajax请求中获取一些数据.
I'm playing with vueJS and trying to grab some data from an ajax request.
这里是我的代码:
new Vue({
el: '#recipeList',
ready: function () {
this.fetchRecipes();
},
methods: {
fetchRecipes: function () {
this.$http.get('/recipes/ajax', function (recipes) {
this.$set('recipes') = recipes;
});
}
}})
html代码很好,我怀疑您需要看到它.
The html code is fine, I doubt you need to see that.
文档说,这就是您执行ajax请求的方式,但是$ http对象似乎未设置.
The documentation says that this is how you do a ajax request, however the $http object does not appear to be set.
这是我收到的控制台错误:
Here is the console error I am receiving:
TypeError: undefined is not an object (evaluating 'this.$http.get')
fetchRecipesapp.js:10
(anonymous function)vue.js:307
readyapp.js:5
_callHookvue.js:8197
readyvue.js:10169
$mountvue.js:10155
_initvue.js:8054
Vuevue.js:80
global codeapp.js:1
app.js:10
推荐答案
$ http.get
适用于Vue资源.确保将其正确拉入.也就是说,将 vue-resource
添加到package.json,然后进行npm install,然后...
$http.get
is for Vue Resource. Make sure you are pulling that in properly. i.e., add vue-resource
to your package.json, then npm install, then...
var Vue = require('vue');
Vue.use(require('vue-resource'));
此外,请确保您的根路径设置正确.
Also, make sure your root path is set up properly.
Vue.http.options.root = '/your-root-path';
希望有帮助!:-)
这篇关于$ http vueJs未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!