【发布时间】:2019-12-02 20:58:31
【问题描述】:
<template>
<router-link :to="{ name: 'work', params: { projectId: $store.state.projectId }}">Work</router-link>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
@Component()
export default class NavBar extends Vue {
public activeIndex: string;
public mounted() {
// TODO: Below name is getting as null
this.activeIndex = this.$route.name;
}
}
</script>
我的路由器定义如下。
const router = new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'home',
component: Home,
},
{
path: '/work/project/:projectId',
name: 'work',
component: () => import('./views/Work.vue'),
},
],
});
当我在浏览器中点击以下网址时,我得到 'this.$route.name' 作为 'home'
当我在浏览器中点击以下网址时,我得到 this.$route.name 为空。
https://localhost/work/project/38
那么,当我点击上面的 url 时,我没有像 this.$route.name 一样得到“工作”,这是怎么回事?
console.log(this.$route) 的输出;
fullPath: "/"
hash: ""
matched: []
meta: {}
name: null
params: {}
path: "/"
query: {}
【问题讨论】:
-
console.log(this.$route)在该对象中显示什么? -
添加有问题
-
这可能是因为
/work/project与/work/project/:projectId模式不完全匹配,因为缺少projectId部分。如果您希望参数是可选的,请进行以下更改/work/project/:projectId->/work/project/:projectId?。 -
我提供了参数'38'
标签: html vue.js vuejs2 vue-router