通過動態路由方式
主要適用於傳遞一個參數的路由跳轉,比如詳情頁。
//路由配置文件中 配置動態路由
{
path: '/detail/:id',
name: 'Detail',
component: Detail
}
//跳轉時頁面
var id = 1;
this.$router.push('/detail/' + id)
//跳轉後頁面獲取參數
this.$route.params.id
通過params獲取參數
Params只能通過name方式跳轉路由。
不配置path參數
第一次獲取路由參數能獲取到,刷新頁面後獲取不到路由參數。
//路由配置文件中 不配置path參數
path: "/projFeedbackDetail",
name: "ProjFeedbackDetail",
component: ProjFeedbackDetail
//跳轉時頁面
this.$router.push({
name: "ProjFeedbackDetail",
params: {
prjId: this.prjId,
cycleId: this.cycleId,
feedbackIdKey: feedbackIdKey
}
});
//跳轉後頁面
this.prjId = this.$route.params.prjId;
this.cycleId = this.$route.params.cycleId;
this.feedbackIdKey = this.$route.params.feedbackIdKey;
配置path參數
第一次獲取路由參數能獲取到,刷新頁面後也能獲取到路由參數。注意參數必須存在,不能爲nul或者‘’。
路由配置頁面改爲
//路由配置文件中 配置path參數
path: "/projFeedbackDetail/:prjId/:cycleId/:feedbackIdKey",
name: "ProjFeedbackDetail",
component: ProjFeedbackDetail
通過query獲取參數
Query可以使用name和path兩種方式跳轉路由.。
//路由配置文件中
{
path: '/detail',
name: 'Detail',
component: Detail
}
//跳轉時頁面
this.$router.push({
path: '/detail',
query: {
name: '張三',
id: 1,
}
})
//跳轉後頁面獲取參數對象
this.$route.query
params和query獲取路由參數的區別
query類似 get, 跳轉之後頁面 url後面會拼接參數,類似?id=1, 非重要性的可以這樣傳, 密碼之類還是用params配置path參數的方式。
params類似 post, 跳轉之後頁面 url後面不會拼接參數 , 但是如果不配置path參數的話刷新頁面參數會消失