vue路由的两种方式,路由传参

query和params区别

  1. query类似 get, 跳转之后页面 url后面会拼接参数,类似?id=1, 非重要性的可以这样传, 密码之类还是用params刷新页面id还在

  2. params类似 post, 跳转之后页面 url后面不会拼接参数 , 但是刷新页面id 会消失

一 . 声明式 router-link (利用标签跳转)

  1.0 不带参数   形如:http://localhost:3000/#/home/newslist
 2 
 3 // router.js 路由配置
 4 { path: '/home/newslist', name:'newslist' component: Newslist }
 5 
 6 <router-link to="/home/newslist">
 7 <router-link :to="{name:'newslist'}"> 
 8 <router-link :to="{path:'/home/newslist'}"> //name,path都行, 建议用name  
 9 
10 // 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始
11 
12 
 1.1带参数   形如(例如从商品进入详情):http://localhost:3000/#/home/newsinfo/13
14 
15 // router.js 路由配置
16 { path: '/home/newsinfo/:id', name: 'newsinfo', component: Newsinfo } //或者
17 { path: '/home/newsinfo:id', name: 'newsinfo', component: Newsinfo }
18 
19 <router-link :to="'/home/newsinfo/' + item.id">
20 <router-link :to="{name:'newsinfo', params: {id:item.id}}"> //router.js用上面的
21 
22 // params传参数 (类似post) 直接跟参数的形式
23 // 不配置path ,第一次可请求,刷新页面id会消失
24 // 配置path,刷新页面id会保留
25 
26 // html 取参  $route.params.id
27 // script 取参  this.$route.params.id
28 
29 1.2带参数 形如(例如从商品进入详情):http://localhost:3000/#/home/newsinfo?id=13
30 
31 // router.js 路由配置
32 { path: '/home/newsinfo', name: 'newsinfo', component: Newsinfo }
33 
34 <router-link :to="'/home/newsinfo?>
35 <router-link :to="'/home/newsinfo?>
36 
37 // query传参数 (类似get,url后面会显示参数)
38 // 路由可不配置
39 // html 取参  $route.query.id
40 // script 取参  this.$route.query.id

 二: 编程式:this.$router.push() (点击事件传id,函数里面接收调用)

1.  不带参数 形如:http://localhost:3000/#/home/newslist

 // router.js 路由配置
{ path: '/home', name:'home' component: Home }

this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})

2.1. params传参   形如(例如从商品进入详情):http://localhost:3000/#/home/13

// router.js 路由配置
{ path: '/home/:id', name: 'home', component: Home } //或者
{ path: '/home:id', name: 'home', component: Home }

this.$router.push('/home/' + id)
this.$router.push({name:'home',params: {id:id}})  // 只能用 name
 
 // params传参数 (类似post) 直接跟参数的形式
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
 
// html 取参  $route.params.id
// script 取参  this.$route.params.id
 
 
2.2. query传参 形如(例如从商品进入详情):http://localhost:3000/#/home?id=13
 
 // router.js 路由配置
{ path: '/home', name: 'home', component: Home }
 
this.$router.push('/home?id=' + id)
this.$router.push({name:'home',query: {id:id}})
this.$router.push({path:'/home',query: {id:id}})
 
// query传参数 (类似get,url后面会显示参数)
// html 取参  $route.query.id
// script 取参  this.$route.query.id

this.$router.replace() (用法同上,push)

this.$router.go(n)