我試圖找到一種方法來為頁面上的query所有router-links添加默認值(在它擁有的所有組件中)?
例如,我希望頁面上的所有鏈接都以引數結尾:utm_campaign=from_our_friends。
并且這個頁面使用了其他頁面也使用的組件。
uj5u.com熱心網友回復:
您可以向頁面組件添加導航守衛,將額外的查詢引數添加到下一個目的地:
beforeRouteLeave(to, from, next) {
const query = { ...to.query, utm_campaign: "from_our_friends" };
next({ query });
}
uj5u.com熱心網友回復:
根據@Majed Badawi 的回答,我最終應用了這個解決方案:
beforeRouteLeave(to, from, next) {
const query = {
...to.query,
utm_campaign: 'from_our_friends'
};
if (!to.query.utm_campaign) { // otherwise you'll get 'infinite redirection' error
next({ path: to.path, query });
} else {
next()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/348563.html
標籤:Vue.js
