vue常见的两钟路由传值方式...
router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'index',
component: Index,
redirect: '/index',
children: [
{
path: '/index',
name: 'Index',
meta:{title: 'a'},
component: () => import('../views/narTable/index.vue'),
},
{
path: '/privilege',
name: 'Privilege',
meta:{title: 'b'},
component: () => import('../views/narTable/privilege.vue'),
},
{
path: '/my',
name: 'My',
meta:{title: 'c'},
component: () => import('../views/narTable/my.vue'),
},
]
]
1.通过 路由名+params (name+params) 的方式传参 (不常用)
//name 为router.js中配置的name,parmars为传递参数,可以传递多个
this.$router.push(name:"My",parmars:{id:1,code:"2"})
在跳转的页面接收时使用this.$route.parmars.参数名来接收!
如: this.id = this.$route.parmars.id
this.code = this.$route.parmars.code
*路由名+params的方式传参会因为页面刷新而数据丢失!
2.通过 路由路径 + query (path+query) 的方式跳转传参
//path 为router.js中配置的path,query为传递参数,可以传递多个
this.$router.push(path :"/my",query:{id:1,code:"2"})
在跳转的页面接收时使用this.$route.query.参数名来接收!
如: this.id = this.$route.query.id
this.code = this.$route.query.code
*路由路径+query 的方式传参不会因为页面刷新而数据丢失!
***注意:传递参数时我们使用 this.$touter ,接收参数时我们使用
我们在通过<router-link> 标签中的to路由跳转时,同样也是通过path+query 和 name+params
<router-link :to = "{ path: '/my', query: { id: 1 } }">跳转到my页面</router-link>
<router-link :to = "{ name: 'My', params: { id: 1 } }">跳转到my页面</router-link>