Vue <router-view>用法

Vue <router-view>用法

  • <router-view>有什么作用

    • <router-view>一般用于路由管理。当我们需要对页面进行局部刷新的时候,就可以使用<router-view>。
  • <router-view>具体使用场景

    • 例如,我们创建了一个侧边导航栏,并且需要使用侧边导航栏对主页面内容进行切换,主页面切换的时候保持侧边导航栏不变。(类似于掘金签到页面)。
  • <router-view>使用方法

    • 主要是需要对router.js进行配置。

      {
        	path: '/router/view',
        	name: 'router:view',
          redirect: '/aPage', //页面将默认加载/aPage路由对应的组件
          children: [
            {
              path: '/aPage',
              name: 'aPage',
              component: () => import('@/components/aPage.vue')
            },
            {
              path: 'bPage',
              name: 'bPage',
              component: () => import('@/components/bPage.vue')
            }
          ]
      }
      

      上面是路由配置文件。

      /router/view是父路由path。

      children数组中是在父页面中进行切换的子路由path。当在父页面组件中使用this.$router.push({path: ‘/bPage’}),就会切换成bPage页面。

    • 父页面组件

      <template>
        <router-view></router-view>
        <SideMenu @clickA = 'handleClickA' @clickB = 'hanldeClickB' />
      </template>
      
      <script>
        import SideMenu from '@/componnets/SideMenu';
        export defineComponents({
          components: {
            SideMenu
          },
          method: {
            handleClickA(){
              this.$router.push({path: '/aPage'})
            },
            handleClickB(){
              this.$router.push({path: '/bPage'})
            }
          }
        })
      </script>
      
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值