watch: {
id (val) {
let query = this.$router.history.current.query;
let path = this.$router.history.current.path;
//對象的拷貝
let newQuery = JSON.parse(JSON.stringify(query));
// 地址欄的參數值賦值
newQuery.id = val;
this.$router.push({ path, query: newQuery });
}
},
我的需求是,點擊切換上下篇文章,我就在地址欄添加了參數,但是切換刷新后,還是原來的;所以就通過在點擊的時候,將值賦值到data里面,然后通過watch監聽,將值動態的換到地址欄上去,這樣刷新就是你點擊的最新的啦