p111 04掌握vue-router-路由的嵌套

学习目标:

路由嵌套使用


学习内容:

路由多层嵌套展示子路由


学习时间:

每天进步一点


学习产出:

路由嵌套实现
第一步:
创建三个组件
1.src/components/home.vue(父路由)

<template>
<div>
  <h1>
    我是首页
  </h1>
  <p>我是首页内容,哈哈哈</p>
  <router-link to="/home/news">新闻</router-link>  <!-- 这里的to不能写成"home/news" 也不能直接是"/news"-->
  <router-link to="/home/message">消息</router-link>
  <router-view></router-view>
</div>
</template>

<script>
export default {
name: "home"
}
</script>

<style scoped>

</style>

2.src/components/homemessage.vue(子路由)

<template>
  <div>
    <ul>
      <li>消息1</li>
      <li>消息2</li>
      <li>消息3</li>
      <li>消息4</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "homemessage"
}
</script>

<style scoped>

</style>

3.src/components/homenews.vue(子路由)

<template>
  <div>
    <ul>
      <li>新闻1</li>
      <li>新闻2</li>
      <li>新闻3</li>
      <li>新闻4</li>
      <li>新闻5</li>
      <li>新闻6</li>
      <li>新闻7</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "homenews"
}
</script>

<style scoped>

</style>

第二步:
路由配置:
src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
/*import home from '../components/home'*/
import about from "../components/about";

const home = () =>import('../components/home')
const homenews = () =>import('../components/homenews')
const homemessage = () =>import('../components/homemessage')

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/home',
      name: 'home',
      component: home,
      children:[{
        path:'news',/*这里的news前不用加/*/
        component: homenews
      },{
        path: 'message',
        component: homemessage
      },{
        path: '',
        redirect: 'news'
      }]
    },{
      path: '/about',
      name: 'about',
      component: about
    },
    {
      path: '/',
      name: 'home',
      component: home,
      redirect: '/home'
    }
  ],
  mode:'history',
  linkActiveClass:'active'
})

第五步 展示:
App.vue

<template>
  <div id="app">
<!--    <router-link to="/home" tag="button" replace >首页</router-link>
    <router-link to="/about" tag="button" replace >关于</router-link>-->
    <button @click="shouclick">首页</button>
    <button @click="guanclick">关于</button>

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

<script>
export default {
  name: 'App',
  methods:{
    shouclick(){
      this.$router.push('/home')
    },
    guanclick(){
      this.$router.replace('/about')
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.active{
  color: #f00;
}
</style>

效果图
在这里插入图片描述
此系列文章源自视频文件总结,尽供学习使用,如有侵权,请联系博主删除,感谢。
视频出处链接:https://www.bilibili.com/video/BV15741177Eh?p=111

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值