【Vue】【Router】手動ジャンプ用this.$router.push()時$router未定義の問題

575 ワード

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

 
転載先:https://www.cnblogs.com/CoderMonkie/p/vue-router-undefined.html