初入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'}) }