
こんにちは、mahです。
このブログでは、
僕がIT未経験から約1年でフリーランスエンジニアになるまでの過程、
ノウハウなどを書いていきます。
今回は、
- 【Vue.js】現在のパス(URL)を取得する【Vue-Router】
について書いていきます。
【Vue.js】現在のパス(URL)を取得する【Vue-Router】
手順
現在のパスは、
this.$route.path
で取得出来る。
- Vue-Routerリファレンス
例、現在のパスによってリダイレクト先を変える関数
# js:some_component.vue
redirectToRoot: function() {
// 現在のパスを取得
console.log(this.$route.path)
if (this.$route.path === '/')
this.$router.push({ path: '/home' })
else
this.$router.push({ path: '/' })
this.$router.go({ path: this.currentRoutePath })
}
}
以上です。