vue后台管理项目中菜单栏切换的三种方法

第一种方法:vue嵌套路由(二)

<el-menu :default-active="defaultActive" style="min-height: 100%;" theme="dark" router>
                        <el-menu-item index="manage">
                            <i class="el-icon-menu"></i>首页
                        </el-menu-item>
                        <el-submenu index='2'>
                            <template slot='title'>
                                <i class="el-icon-document"></i>用户管理
                            </template>
                            <el-menu-item index="userList">用户查询</el-menu-item>
                            <el-menu-item index="userforbid">封号管理</el-menu-item>
                            <el-menu-item index="userapply">用户申请</el-menu-item>
                        </el-submenu>
                    </el-menu>

<el-col :span="20" style="min-height:100%;background:#F2F4F8">
  <router-view></router-view>
</el-col>

在js中

{
        path:'/manage',
        component: manage,
        name:'',
        children:[{
            path:'/userList',
            component:userList,
            meta: ['用户列表']
        }]
    }

当我们点击用户查询时,就会跳转到http://localhost:8080/#/userList

第二种方法点击事件

<el-button :plain="true" type="success" @click="goadd">    



  goadd(){
              this.$router.push({ path: '/addcategory' })
          }

第三种方法命名路由:

<div id="app">  
    <h1>Named Routes</h1>  
    <p>Current route name: {{ $route.name }}</p>  
    <ul>  
        <li><router-link :to="{ name: 'home' }">home</router-link></li>  
        <li><router-link :to="{ name: 'foo' }">foo</router-link></li>  
        <li><router-link :to="{ name: 'bar', params: { id: 123 }}">bar</router-link></li>  
    </ul>  
    <router-view class="view"></router-view>  
</div>  
  
<template id='home'>  
    <div>This is Home</div>  
</template>  
  
<template id='foo'>  
    <div>This is Foo</div>  
</template>  
  
<template id='bar'>  
    <div>This is Bar {{ $route.params.id }}</div>  
</template>  





const Home = { template: '#home' };  
const Foo = { template: '#foo' };  
const Bar = { template: '#bar' };  
  
const router = new VueRouter({  
    routes: [  
        { path: '/', name: 'home', component: Home },  
        { path: '/foo', name: 'foo', component: Foo },  
        { path: '/bar/:id', name: 'bar', component: Bar }  
    ]  
});  
  
new Vue({ router:router }).$mount('#app');  

第四种方法命名视图:

<div id="app">  
    <router-link to="/">Go to Foo</router-link>  
    <router-view class="view one"></router-view>  
    <router-view class="view two" name="a"></router-view>  
    <router-view class="view three" name="b"></router-view>  
</div>  
  
<template id='foo'>  
    <div>This is Foo</div>  
</template>  
  
<template id='bar'>  
    <div>This is Bar {{ $route.params.id }}</div>  
</template>  
  
<template id='baz'>  
    <div>This is baz</div>  
</template>  


const Foo = { template: '#foo' };  
const Bar = { template: '#bar' };  
const Baz = { template: '#baz' };  
  
const router = new VueRouter({  
    routes: [  
        {  
            path: '/',  
            components: {  
                default:Foo,  
                a:Bar,  
                b:Baz  
            }  
        }  
    ]  
});  
  
new Vue({ router:router }).$mount('#app');  

第五种方法

<div id="app">  
    <h1>Hello App!</h1>  
    <p>  
        <!-- 使用 router-link 组件来导航. -->  
        <!-- 通过传入 `to` 属性指定链接. -->  
        <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->  
        <router-link to="/foo">Go to Foo</router-link>  
        <router-link to="/bar">Go to Bar</router-link>  
    </p>  
    <!-- 路由出口 -->  
    <!-- 路由匹配到的组件将渲染在这里 -->  
    <router-view></router-view>  
</div>  
  
<template id='foo'>  
    <p>this is foo!</p>  
</template>  
<template id='bar'>  
    <p>this is bar!</p>  
</template>  


// 1. 定义(路由)组件。  
// 可以从其他文件 import 进来  
const Foo = { template:'#foo' };  
const Bar = { template:'#bar' };  
// 2. 定义路由  
// 每个路由应该映射一个组件。 其中"component" 可以是  
// 通过 Vue.extend() 创建的组件构造器,  
// 或者,只是一个组件配置对象。  
const routes = [  
    { path: '/foo', component: Foo },  
    { path: '/bar', component: Bar }  
];  
// 3. 创建 router 实例,然后传 `routes` 配置  
// 你还可以传别的配置参数, 不过先这么简单着吧。  
const router = new VueRouter({ routes:routes });  
// 4. 创建和挂载根实例。  
// 记得要通过 router 配置参数注入路由,  
// 从而让整个应用都有路由功能  
const app = new Vue({ router:router }).$mount('#app');  

引:http://blog.csdn.net/bboyjoe/article/details/52804988

原文地址:https://www.cnblogs.com/myzy/p/7112368.html