Vue 參數傳遞及刷新後依舊存在
阿新 • • 發佈:2017-06-14
sent 多個 com col tar path .... ram ref
獲取參數方式有兩種:
1、params
2、query
第一種方式: params
this.$router.push({name:‘Hello‘,params:{name:‘zs‘,age:‘22‘}});
name:組件中的命名
params 中兩個參數分別為name,age
跳轉 hello這個組件,獲取參數值:
var name = this.$route.params.name;
var age = this.$route.params.age;
這樣就會獲取到相應參數
瀏覽器地址為
http://localhost:8080/#/hello/
問題來了 ,刷新參數不見了如何解決往下看
在router路由中有個path
.....
{ path:‘/hello/:name/:age‘, name:‘Hello‘, component: Hello }
這裏要使用 /:name 如果有多個可以這樣 /:name/:age/.......
這是瀏覽器 顯示
http://localhost:8080/#/hello/zs/22
zs ,22就是值。
第二種方法:
query
跳轉URL攜帶參數
this.$router.push({name:‘Hello‘,query:{name:‘zs‘,age:‘22‘}});
在組件中獲取
var name= this.$route.query.name; var age = this.$route.query.age;
瀏覽器會顯示
http://localhost:8080/#/?name=zs&age=22
兩種方式都可以解決刷新參數不見問題。
跳轉鏈接
Vue 參數傳遞及刷新後依舊存在