2016-09-08 25 views
2

如何從愛可信調用一個函數然後下面是我的代碼是不工作axios內部的函數調用呢?

handleClick(userid){ 
    axios.get(
    "http://localhost:4000/user/"+userid+"/items.json") 
    .then(function (response) { 
     dispatch(this.buildhtml.bind(response)) 
    }) 
} 


    buildhtml(response){ 
    console.log("m called") 
    } 

buildhtml功能不執行!任何想法

回答

3

您的代碼不能正常工作,因爲您的this將與您當前的實現未定義。

你可以試試嗎?

handleClick(userid){ 
    var self=this; 
    axios.get(
    "http://localhost:4000/user/"+userid+"/items.json") 
    .then(function (response) { 
     self.buildhtml.bind(response) // would work 
     dispatch(self.buildhtml.bind(response)) //wont work 
    }) 
} 

    buildhtml(response){ 
    console.log("m called") 
    } 

現在我看到上面不會工作太多,即使你改變它自我。你正在嘗試使用派遣。在發貨中,您需要通過操作。。 Reducers將狀態和操作作爲參數,並根據傳遞的操作更新狀態。

現在一個動作可能會返回一個對象或函數。請仔細閱讀redux的概念。這不是一個行動應該發出的方式

+0

沒有工作:( – ashwintastic

+0

你可以分享一次你的組件嗎?創建一個小提琴並共享鏈接? –

+0

請檢查此鏈接[fiddle鏈接](https://jsfiddle.net/ob1c0aye/1/) – ashwintastic

1

當使用Vue.jsaxios,我也有這個問題,但是,這是我如何解決它。

let vue = new Vue({ 
el:#<element>, 
methods:{ 

    method1() 
    { 
axios.post('<url>',{<data>}) 
.then((res) => { 
    this.method2(res) // method call to method2 
    }) 
.catch((err) => { <call> });  
    },//method1 end 

method2(res){ 
// statements 
} 

} 
}); 

參照this