2015-10-20 56 views
9

我正在玩vueJS並試圖從ajax請求中獲取一些數據。

繼承人我的代碼:

new Vue({ 
el: '#recipeList', 

ready: function() { 
    this.fetchRecipes(); 
}, 

methods: { 
    fetchRecipes: function() { 
     this.$http.get('/recipes/ajax', function (recipes) { 

      this.$set('recipes') = recipes; 

     }); 
    } 
}}) 

的HTML代碼是好的,我懷疑你需要查看。

該文檔說,這是你如何做一個ajax請求,但$ http對象似乎沒有設置。

這是我收到的控制檯錯誤:

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 

回答

18

$http.getVue Resource。確保你正確地拉動它。即,添加vue-resource到您的package.json,那麼NPM安裝,然後...

var Vue = require('vue'); 

Vue.use(require('vue-resource')); 

此外,請確保您的根路徑設置正確。

Vue.http.options.root = '/your-root-path'; 

希望它有幫助! :-)