路由懒加载以及嵌套路由

懒加载

const Home = () => import('../components/Home')
const About = () => import('../components/About')
const User = () => import('../components/user')

嵌套路由

  1. 在components下创建两个子路由
    在这里插入图片描述

  2. 在子路由中放一些内容
    在这里插入图片描述

  3. 在index.js中配置

const News = () => import('../components/HomeNews')
const Message = () => import('../components/HomeMessage')
const routes = [{
  path: '/',
  // redirect 重定向
  redirect: '/home'
},
{
  // 浏览器地址栏拼接的地址
  path: '/home',
  component: Home,
  children: [{
    path: 'news',
    component: News
  }, {
    path: 'message',
    component: Message
  }]
}]
  1. 在home.vue中添加
    在这里插入图片描述
    效果
    在这里插入图片描述
  2. 默认显示子组件内容
    在index.js中配置
const routes = [{
  path: '/',
  // redirect 重定向
  redirect: '/home'
},
{
  // 浏览器地址栏拼接的地址
  path: '/home',
  component: Home,
  children: [{
    path: '',
    redirect: 'news'
  }, {
    path: 'news',
    component: News
  }, {
    path: 'message',
    component: Message
  }]
}]

传参----query

  1. 新建一个组件
  2. 在index.js中将组件引入
  3. 在APP.vue中使用组件
<router-link
      :to="{
        path: '/profile',
        query: { name: 'zzzy', age: 18, height: '1.8' }
      }"
      >档案</router-link>
  1. 在子组件中接收参数$route.query.name
<h2>档案</h2>
    <h2>{{ $route.query.name }}</h2>
    <h2>{{ $route.query.age }}</h2>
    <h2>{{ $route.query.height }}</h2>

在这里插入图片描述
或者

  1. 新建一个组件
  2. 在index.js中将组件引入
  3. 在APP.vue中使用组件
<button class="btn" @click="check">档案</button>
  methods: {
    check() {
      // this.$router.replace("/profile");
      this.$router.replace({
        path: "/profile",
        query: {
          name: "zy",
          id: this.userId,
          height: "1.8",
          age: 18
        }
      });
    }
  }
  1. 在子组件中接收参数$route.query.name
    <h2>档案</h2>
    <h2>{{ $route.query.name }}</h2>
    <h2>{{ $route.query.age }}</h2>
    <h2>{{ $route.query.height }}</h2>
    <h2>{{ $route.query.id }}</h2>

在这里插入图片描述

传参-------params

  1. 新建一个组件

  2. 在index.js中将组件引入,且配置
    在这里插入图片描述

  3. 在APP.vue中使用组件
    在这里插入图片描述

  4. 子组件内接收参数$route.params.userId
    在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值