解決vue路由跳轉(zhuǎn)頁(yè)面不刷新(頁(yè)面變量加載不自動(dòng)刷新)問(wèn)題的兩種方法

路由參數(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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容