如何仅在某些 Vue.js 组件上显示导航栏元素?
How to show navbar element only on certain Vue.js components?
我在 3 部分组件中使用 Vue.js 应用和 vue-router:
App.vue:
<template>
<div id="app">
<head>
</head>
<NavbarComp/>
<div>
<div class="middle">
<router-view/>
</div>
</div>
<FooterComp/>
</div>
</template>
<NavbarComp/>
包含一个登录按钮,我希望它只出现在登陆页面上:
<template>
<div>
<nav>
<router-link to="/" >
<h3>My Shining app </h3>
</router-link>
<router-link to="/login">
<button v-if="section='landing'"> Login</button>
</router-link>
</nav>
</div>
</template>
我试图在商店中定义一个 section
,并将该部分定义为每个组件上的计算 属性:
section () {
this.$store.section = 'login';
}
但未能成功。所以感谢你的提示。
将按钮上的 v-if
更改为:
v-if="this.$route.name === 'whatever-your-route-name-is'" // 'landing' according to your comment
我在 3 部分组件中使用 Vue.js 应用和 vue-router:
App.vue:
<template>
<div id="app">
<head>
</head>
<NavbarComp/>
<div>
<div class="middle">
<router-view/>
</div>
</div>
<FooterComp/>
</div>
</template>
<NavbarComp/>
包含一个登录按钮,我希望它只出现在登陆页面上:
<template>
<div>
<nav>
<router-link to="/" >
<h3>My Shining app </h3>
</router-link>
<router-link to="/login">
<button v-if="section='landing'"> Login</button>
</router-link>
</nav>
</div>
</template>
我试图在商店中定义一个 section
,并将该部分定义为每个组件上的计算 属性:
section () {
this.$store.section = 'login';
}
但未能成功。所以感谢你的提示。
将按钮上的 v-if
更改为:
v-if="this.$route.name === 'whatever-your-route-name-is'" // 'landing' according to your comment