我想将标头文字更改为我所在的当前路线名称。它仅适用于组件安装,单击时不触发。如何通过路由器创建的单击按钮使changeHeaderTitle运行?
<template>
<div>
<nav class="navigation--navbar">
<router-link
class="navigation--links"
v-for="routes in links"
v-bind:key="routes.id"
@click='changeHeaderTitle'
:to="`${routes.page}`"
>{{routes.text}}</router-link>
</nav>
<div class="header--headline">
<h2 class="header--headline-text">{{headerTitle}}</h2>
</div>
</div>
</template>
<script>
export default {
name: 'Navigation',
data () {
return {
headerTitle: 'Boop',
links: [
{
id: 0,
text: 'Home',
page: '/Home'
},
{
id: 1,
text: 'Tutoring',
page: '/Tutoring'
}
]
}
},
methods: {
changeHeaderTitle: function () {
if (this.$route.path === '/') {
this.headerTitle = 'Home Header Here'
} else {
let routeName = this.$route.path.split('/')
this.headerTitle = routeName[1]
}
}
},
mounted: function () {
this.changeHeaderTitle()
}
}
</script>
答案 0 :(得分:1)
好像您需要添加.native
修饰符。做:
<router-link
class="navigation--links"
v-for="routes in links"
v-bind:key="routes.id"
@click.native='changeHeaderTitle'
:to="`${routes.page}`"
>
{{routes.text}}
</router-link>