zoukankan      html  css  js  c++  java
  • vue路由--网站导航功能

    1、首先需要按照Vue router支持

     npm install vue-router
    然后需要在项目中引入:
    import Vue from 'vue'
    import VueRouter from 'vue-router'
    Vue.use(VueRouter)

    2、定义router的js文件

    import Vue from 'vue'
    import Router from 'vue-router'
    import User from '../pages/user'
    import Home from '../pages/public/home'
    import Profile from '../pages/user/profile'
    import Form from '../pages/form'
    import Detail from '../pages/form/form'
    import File from '../pages/form/file'
    import Files from '../pages/file'
    
    Vue.use(Router)
    
    export default new Router({
        routes: [
            { path: '/', component:Home,
                children:[
                    { path: '/user', component:Profile},
                    { path: '/profile', component: User},
                    { path: '/form', component: Form},
                    { path: '/detail', component: Detail},
                    { path: '/profiles', component: Files},
                    { path: '/file', component: File}
                ]
            },
           { path: '/login', component:Login},
    { path: '/404', component:Error}
       ] 
    })

    3、在main.js中引入router

    import router from './router'
    
    new Vue({
      router,
      render: h => h(App),
    }).$mount('#app')

    4、入口页面定义router-view

    <div id="app">
        <router-view></router-view>
      </div>

    5、在path指向为“/”的页面中,定义页面的布局,例如:上(头部)-中(左道航-右内容)-下(底部)。

    <HeaderSection></HeaderSection>
       <div>
          <NavList class="nav"></NavList>
          <router-view  class="router"></router-view>
       </div>
    <FooterSection></FooterSection>

    6、左侧导航,用elementUI实现,有一个NavMenu导航菜单,做导航功能。

    在这里提一下引入elementUI:

    (1)安装

        npm i element-ui -S

    (2)使用

      在main.js中加入下面的代码:

      import ElementUI from 'element-ui';

      import 'element-ui/lib/theme-chalk/index.css';

      Vue.use(ElementUI);

    导航栏的代码如下:

    <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
                     text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
        <template v-for="item in items">
           <template v-if="item.subs">
              <el-submenu :index="item.index" :key="item.index">
                 <template slot="title">
                   <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
                 </template>
                 <template v-for="subItem in item.subs">
                    <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
                       <template slot="title">{{ subItem.title }}</template>
                       <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">
                         {{ threeItem.title }}
                       </el-menu-item>
                    </el-submenu>
                    <el-menu-item v-else :index="subItem.index" :key="subItem.index">
                       {{ subItem.title }}
                    </el-menu-item>
                 </template>
             </el-submenu>
          </template>
          <template v-else>
              <el-menu-item :index="item.index" :key="item.index">
                  <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
              </el-menu-item>
          </template>
      </template>
    </el-menu>

    定义左侧导航的显示和图标等内容,index为唯一标识,打开的是path路径,对应router中的path,就可以打开写好的相应的页面。

               items: [
                        {
                            icon: 'el-icon-share',
                            index: 'user',
                            title: '系统首页'
                        },
                        {
                            icon: 'el-icon-time',
                            index: 'profile',
                            title: '基础表格'
                        },
                        {
                            icon: 'el-icon-bell',
                            index: '3',
                            title: '表单相关',
                            subs: [
                                {
                                    index: 'form',
                                    title: '基本表单'
                                },
                                {
                                    index: '3-2',
                                    title: '三级菜单',
                                    subs: [
                                        {
                                            index: 'detail',
                                            title: '富文本编辑器'
                                        },
                                        {
                                            index: 'file',
                                            title: 'markdown编辑器'
                                        },
                                    ]
                                },
                                {
                                    index: 'profiles',
                                    title: '文件上传'
                                }
                            ]
                        },
                    ]

    7、如果涉及到登录页面和不需要路由的页面等,就需要在router的js文件中定义和“/”平级的其他path的页面,再判断进入页面是路由页面还是登录等页面。

  • 相关阅读:
    二维动态规划(2)
    细节是否真的打败爱情,十年后你还会爱我吗?
    C++的四种cast操作符的区别类型转换
    纯虚函数
    二维动态规划
    1,2,...n n个数m个丢失,找出丢失的数
    虚拟内存管理技术
    C++ 面试题总结
    【转】图的邻接链表 adjacent list of graph
    CIOCPServer的数据结构定义及内存池方案
  • 原文地址:https://www.cnblogs.com/zjingjing/p/10530792.html
Copyright © 2011-2022 走看看