Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

vue router导航栏到底该怎么加?

细致描述一下我的问题:
我的需求其实很简单,但是被vue-router 的router view搞晕了

需求是:做一个导航栏,点击导航,其下方内容改变(也就是切换 一个view)

最开始我是这样想的

在App.vue 文件里面 直接写上 导航如下
图片描述

App.vue大致代码如下
<router-link to="/">单音</router-link>
<router-link to="/multi">多轨</router-link>
<router-link to="/manageApp">混音</router-link>



<router-view></router-view>

然后 将 这几个link对应的页面设置成 App.vue路径的chirden

就像这样

const routers = [{
  path: '/',
  component: App,
  // meta: { requiresAuth: true },
  children: [{
    path: '/home',
    component: Home,
    children:HomeChild
  }, {
    path: '/manageApp',
    component (resolve) {
      require.ensure(['../views/manage_app.vue'], () => {
        resolve(require('../views/manage_app.vue'))
      })
    },
    children:ManageAppChild
  }, {
    path: '/ManageConfig',
    component (resolve) {
      require.ensure(['../views/manage_config.vue'], () => {
        resolve(require('../views/manage_config.vue'))
      })
    },
    children: ManageConfigChild
  },{
    path: '/publishApp',
    component (resolve) {
      require.ensure(['../views/publish_app.vue'], () => {
        resolve(require('../views/publish_app.vue'))
      })
    },
    children: publishAppChild
  }, {
    path: '/threat-perception',
    name: 'threat-perception',
    component (resolve) {
      require.ensure(['../views/manage_app.vue'], () => {
        resolve(require('../views/manage_app.vue'))
      })
    }
  }]
}

但是,这样点击App里面的导航的时候,router-view里面又会渲染出带导航栏的页面,相当于再渲染了一次App.vue,我想想好像也是这个道理,点击link之后 router-view里面渲染 点击出来的页面,如果这个页面是谁的子页面,那就连带父集一起渲染了,但是这样的逻辑,反正我又晕了。。。

如何才能解决这个问题呢?
我看了别人的实例,他们是这样定义的

App.vue里面只有一个router view

<template>
<router-view></router-view>
</template>

然后 将默认页面指向了一个带导航条的页面,页面里面 也有一个router-view
点击导航条,下方就会切换到对应的子页面。。。。

const routers = [{
  path: '/',
  component: require('../views/top_menu.vue'),
  children:[
    {
      path: '/',
      component:require('../views/singleAudio.vue')
    },

    {
      path: '/multi',
      component:require('../views/multi_audio.vue')

    },

    {
      path: '/manageApp',
      component:require('../views/multi_audio.vue')
    },

    {
      path: '/ManageConfig',
      component :require('../views/singleAudio.vue')
    },
    {
      path: '/publishApp',
      component:require('../views/singleAudio.vue')
    },

    {
      path: '/threat-perception',
      name: 'threat-perception',
      component :require('../views/singleAudio.vue')
    }
    ]
}

]

大概设置如图,但是我觉得跟我的差不多啊。。。。。、为啥他的就不会出现我那种重复渲染的现象。。。。。
求大神,解答疑惑,谢谢了%>_<%


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

不要用 children,children 是在当前 router-view 里面的 router-view 渲染


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...