【Vue】3.vue2.0嵌套路由-params传递参数

在嵌套路由中,父路由向子路由传值,除了query外,还有params,params传值有两种情况,一种是值在url中显示,另一种是值不显示在url中。

(1)显示在url中

apple.vue

<template>
    <div>
      <div>
        {{msg}}
        {{$route.params.num}}
        <button @click="getParam">getParam</button>
      </div>
      <div>
        <router-view></router-view>
      </div>
      <div>
        <router-link :to="{path:'/apple/apple1'}">apple1</router-link>
        <router-link :to="{path:'/apple/apple2'}">apple2</router-link>
      </div>
    </div>
</template>

<script>
    export default{
        name:'Apple',
        data(){
            return {
                msg: 'hello apple'
            }
        },
      methods:{
        getParam(){
          console.log(this.$route.params.num);
        }
      }
    }
</script>

<style>
</style>
router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Apple from '@/components/apple'
import Banana from '@/components/banana'
import Apple1 from '@/components/Apple1'
import Apple2 from '@/components/Apple2'

Vue.use(Router)

export default new Router({
  mode:'history',
  routes: [
    {
      path: '/',
      redirect: 'helloworld'
    },
    {
      path: '/helloworld',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/apple/:num', //参数在路由里面设置
      name: 'Apple',
      component: Apple,
      children:[
        {
          path:'apple1',
          name:'Apple1',
          component:Apple1
        },
        {
          path:'apple2',
          name:'Apple2',
          component:Apple2
        }
      ]
    },
    {
      path: '/banana',
      name: 'Banana',
      component: Banana
    }
  ]
})



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值