vue.js - 在 VUE.js 中隐藏某些页面的侧边栏组件
问题描述
我正在使用 Vue.js,我有一个问题。
有没有办法sidebar
只隐藏某个页面的组件?这是我下面的代码。
路由器.js
const routes = [
{
path: '/',
redirect: '/home',
component: () => import('@/layout/MainLayout'),
children: [
{
path: 'home',
name: 'Home',
component: () => import('Views/Home.vue'),
},
],
},
{
path: '/subpage',
redirect: '/subpage/subpage',
component: () => import('@/layout/MainLayout'),
children: [
{
path: 'subpage',
name: 'Subpage',
component: () => import('Views/Subpage/Subpage.vue'),
},
],
},
];
主布局.vue
<template>
<div id="content" class="content">
<router-view></router-view>
<Sidebar></Sidebar>
</div>
</template>
<script>
import Sidebar from '@/components/Sidebar.vue';
export default {
components: {
Sidebar,
},
};
</script>
我需要制作另一个布局吗?喜欢MainLayout2.vue
请帮忙。
解决方案
您可以将元信息添加到 vue 路由。将侧边栏键添加到要隐藏的路线
{
path: '/subpage',
redirect: '/subpage/subpage',
component: () => import('@/layout/MainLayout'),
children: [
{
path: 'subpage',
name: 'Subpage',
meta:{sidebar:false}
component: () => import('Views/Subpage/Subpage.vue'),
},
],
},
然后在你的布局组件中,创建一个计算属性来检查当前路由是否需要隐藏侧边栏
computed:{
shouldShowSidebar(){
return this.$route.meta.sidebar!==false;
}
}
然后使用此计算属性有条件地隐藏特定路线上的侧边栏
<Sidebar v-if="shouldShowSidebar"></Sidebar>
您可以添加meta:{sidebar:false}
到任何希望隐藏侧边栏的路线
推荐阅读
- haskell - 在haskell中字符串到[Char]
- sql - 双左连接中的重复列 - bigquery sql
- python - apache气流窗口中的botocore错误
- sorting - PrimeReact DataTable 使用 sortFunction 对列进行预排序
- javascript - 具有多个通用参数的通用反应功能组件
- javascript - 使用 scss 文件时出现“未定义变量”的汇总插件 vue 错误
- c# - 如何在 C# C-Sharp 中打开网络共享上的 SQLite3
- c - 在 main() 中创建的结构变量的范围和用户定义的函数?
- amazon-web-services - 如何查看来自负载均衡器的 2 个实例之间路由的流量?
- lua - 后缀评估不正确