zoukankan      html  css  js  c++  java
  • vue-router传参

    本文介绍利用props传参,传参的形式有三种:布尔模式、对象模式、函数模式

    <router-link :to="{name:'children',params:{id:msg}}">切换咯</router-link>

    布尔模式将props属性设置成为true,如果 props 被设置为 trueroute.params 将会被设置为组件属性。

    {
            path: '/index',
            component:() => import('../src/components/index/index.vue'), 
            children: [{
                path: '/children/:id',
                name:'children',
                component:() => import('../src/components/index/children/index.vue'),
                props: true,  //关键代码
            }]
        },

    //index/children.vue
    export default{
      props:['id']
    }

    对象模式在路由配置(router.js)里边设置props为对象{id:”msg”},然后去组件中设置props,props的值为路由配置里边的props的key,也就是id

        {
            path: '/index',
            component:() => import('../src/components/index/index.vue'), 
            children: [{
                path: '/children',
                name:'children',
                component:() => import('../src/components/index/children/index.vue'),
                props: {
                    id:'msg'
                },
            }]
        },

    注意:这样设置只使用与props里面的值是静态的

    函数模式可以创建一个函数返回 props。这样你便可以将参数转换成另一种类型,将静态值与基于路由的值结合等等

        {
            path: '/index',
            component:() => import('../src/components/index/index.vue'), 
            children: [{
                path: '/children',
                name:'children',
                component:() => import('../src/components/index/children/index.vue'),
                props:()=>({id:'msg1'}),  //静态设置
            }]
        },
        {
            path: '/index',
            component:() => import('../src/components/index/index.vue'), 
            children: [{
                path: '/children',
                name:'children',
                component:() => import('../src/components/index/children/index.vue'),
                props:(route)=>({id:route.params.id}),  //动态设置
            }]
        },
  • 相关阅读:
    “花田喜事” 婚庆网站策划
    discuz 模块模板标签说明 DIY模块模板语法详解
    discuz x2.5 广告位开发学习(第二步:制作)
    DiscuzX2.5完整目录结构【模板目录template】
    Webservice 安全性访问
    X2.5 怎么关闭个人空间
    心中有佛,看人即佛;心中有屎,看人即屎
    discuz x2.5 广告位开发学习(第一步:摸索)
    UVA 128 Software CRC
    UVA 10791 Minimum Sum LCM
  • 原文地址:https://www.cnblogs.com/peilin-liang/p/12015804.html
Copyright © 2011-2022 走看看