路由參數(shù)改變,頁(yè)面數(shù)據(jù)不更新解決方法:
http://localhost:8080/#test?id=1
http://localhost:8080/#test?id=2,參數(shù)切換后,數(shù)據(jù)未更新
vue-router同路由$router.push不跳轉(zhuǎn)一個(gè)簡(jiǎn)單解決方案
vue-router跳轉(zhuǎn)一般是這么寫(xiě):
toCurrentPage: function(thisId){
this.$router.push({path:'/test ', query: { id: thisId, option: ""}});
}
但是當(dāng)遇到,需要跳轉(zhuǎn)同頁(yè)面不同query的情況,上面的方法不起作用。當(dāng)然了,從性能來(lái)說(shuō),理論上這種情況最佳的解決方案,是把需要刷新的包裹成一個(gè)init function,跳轉(zhuǎn)的方法,傳參再次調(diào)用init方法。balabalabala……不在贅述。
但是另一些情況,基本頁(yè)面所有組件都需要刷新,再次加載對(duì)開(kāi)銷影響不大,需要history.back以保證操作邏輯順暢……我們只要跳轉(zhuǎn)加載如此而已,那么其實(shí)也很簡(jiǎn)單,只需在上述方法基礎(chǔ)上加上:
watch: {
'$route' (to, from) {
this.$router.go(0);
}
},
下面是實(shí)際使用有效方法二則:
方法一
通過(guò)路由傳參跳轉(zhuǎn)界面,頁(yè)面沒(méi)有刷新
解決方法:在 router-view 中加 :key="$route.fullPath"
<router-view :key="$route.fullPath"></router-view>
方法二
再跳轉(zhuǎn)后的路由觀察路由變化,進(jìn)行頁(yè)面刷新(這種方法相對(duì)來(lái)說(shuō)會(huì)加載慢一些,用戶體驗(yàn)差)。
watch: {
'$route' (to, from) {
this.$router.go(0);
}
}
附一個(gè)完整頁(yè)面示例:
<template>
<div>
<div class="container">
<ul class="list-unstyled">
<li v-for="item in list" :key="item.GeneralID"><a href="javascript:;" @click="navTo(item)">{{item.Title}}</a></li>
</ul>
</div>
</div>
</template>
<script>
export default {
data: function() {
var model = {
list: []
};
var nid = this.$route.params.nid;
this.jsp("content_list", {"nid": nid}).then((ret) => {
model.list = JSON.parse(ret.result);
});
return model;
},
methods: {
navTo:function(item){this.$router.push("/Content/"+item.GeneralID);}
},
watch: {
'$route' (to, from) {
this.$router.go(0);
}
}
}
</script>