【Vue】【Router】手動跳轉用 this.$router.push() 時 $router 未定義的問題


初入Vue,手寫路由跳轉時的問題:

 

toXxxRoute: () => {
  this.$router.push({'path': '/xxx', 'name': 'xxx'})
}

由於使用了箭頭函數,this的指向與傳統 js 里不同

所以報錯

TypeError: Cannot read property '$router' of undefined

 

與其保持一個this變量,不如改回寫 function 的方式

toXxxRoute: function(){
  this.$router.push({'path': '/xxx', 'name': 'xxx'})
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM