【发布时间】:2018-07-09 11:05:23
【问题描述】:
我在 vue 组件文件中有如下链接。
<li v-for="(word,index) in allWord" v-bind:key="index">
<router-link :to="{ name: 'word', params: {id: word.id } }">
{{word}}
</router-link>
</li>
我在index.js 文件中的路线设置如下
export default new Router({
mode: 'history',
routes: [
{
path: '/',
component: MainBody
},
{ path: '/word/:id',
name: 'word',
component: Word
}
]
});
我得到如下网址
http://localhost:8080/word/4
我正在尝试在word.vue 中捕获 URL 参数,如下所示
<script>
export default {
watch: {
'$route': 'routeChanged'
},
methods: {
routeChanged () {
console.log(this.$route.params.id);
}
}
}
</script>
问题是当我点击<li></li> 时,console 没有任何价值,当我第二次点击时我获得了价值。
为什么会这样?我想第一次获得价值。
【问题讨论】:
标签: vuejs2 vue-router