this$routerpush传参不显示在路径上
在Vue Router中,可以使用params选项将参数传递给路由,并且这些参数不会显示在路径中。
首先,在定义路由时,需要使用props: true来允许将参数作为组件的属性传递:
const routes = [
{
path: '/example',
name: 'example',
component: ExampleComponent,
props: true
}
]
然后,在组件中,可以通过$route.params来获取传递的参数:
export default {
name: 'ExampleComponent',
mounted() {
console.log(this.$route.params); // 获取传递的参数
}
}
最后,在使用this.$router.push进行路由跳转时,可以将参数作为第二个参数传递:
this.$router.push({
name: 'example',
params: {
id: 1,
name: 'example'
}
})
在上面的示例中,将参数id和name传递给了名为example的路由,并且这些参数不会显示在路径中。在ExampleComponent组件中,可以通过this.$route.params获取到传递的参数
原文地址: https://www.cveoy.top/t/topic/iZzc 著作权归作者所有。请勿转载和采集!