Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

vue路由传参的问题

image.png

index.vue:
<button>显示1和2</button> // 点击则会跳转到path:/a的页面
<button>只显示2</button> // 点击则会跳转到path:/a的页面
<router-view></router-view>
router.js:
{
    path: '/a',
    name: 'a',
    component: () => import('@/views/a.vue'),
},

如果第二个按钮使用to='/a?only2=true',在a.vue里通过this.$route.query能获取到,但是信息会暴露在地址栏

求解,有没有能直接在router.js文件里面传递params的操作,类似this.$router.push({path:'/a',params:{only2:true}})这种效果的操作

像是修改第二个按钮的逻辑,让他跳转到 path:'/middle'

在router.js里添加
{
    path:'/middle',
    redirect:'/a',
    params:{
        only2:true
    }
}

或者说有没有能不通过地址栏传参的形式,在a.vue能拿到特定的标识(only2:true)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
可以通过增加一个中间路径来实现
{
    path:'/middle',
    redirect:{path:'/a',params:{only2:true}}
}

缺点是如果原路径就是/a,那么就不会因为这个params而重新加载a.vue


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...