Vue解决路由切换刷新问题

192 篇文章 4 订阅

法一

1.App.vue

<!-- App.vue根组件代码 -->
<template>
  <div class="app">
      <div class="content">
      	   <router-view v-if="routerAlive"></router-view>
      </div>
  </div>
</template>
 
<script>
    export default{
	data(){
	    return {
		routerAlive:true
	    }
	},
	provide(){    //在父组件中创建属性
            return {
                routerRefresh: this.routerRefresh
            }
        },
	methods:{
	    routerRefresh(){
	        this.routerAlive = false;
		this.$nextTick(()=>{
		    this.routerAlive = true;
		});
	    }
	}
    }
</script>

2.use.vue

<style lang="scss" scoped>
    .history-casting{
        background: #f7f7f7;
        padding: 10px 14px;
        .history-casting-box{
            background: #fff;
            padding: 11px 14px 18px 15.5px;
            .history-casting-title{
                font-size: 16px;
                padding: 3px 0;
            }
            .history-casting-list{
                color: #33499C;
                font-size: 12px;
                .history-casting-item{
                    padding: 6px 0;
                }
                .active{
                    color: rgb(185, 9, 1);
                    font-weight: bold;
                }
            }
        }
    }
</style>

<template>
    <div class="history-casting">
        <div class="history-casting-box">
            <div class="history-casting-title">
                历史播报
            </div>
            <div class="history-casting-list">
                <div  class="history-casting-item" v-for="(item,index) in historyCastingList" 
                    :key="index" :class="{'active':ind === index}" @click="changeBgc(index)">
                    {{item}}
                </div>
            </div>
        </div>
    </div>
</template>
<script>
//Vuex公共状态动作
export default {
	inject:['routerRefresh'],   //在子组件中注入在父组件中出创建的属性
    props:{
        //微信用户ID
        openid:{
            type:String,
        },
        //是否是历史数据
        historyid:{
            type:String
        },
        //时间戳
        todaytime:{
            type:String
        },
        //理财经理ID
        userid:{
            type:String,
        },
        //是否为分享文章
        isShare:{
            type:String,
        },
        //小程序
        isWXApplet:{
            type:String
        },
        //播报id
        fieldid:{
            type:String
        },
    },
    data() {
        return {
            historyCastingList:[
                '晚间播报(2019年8月14日星期日 8点)',
                '晚间播报(2019年8月14日星期日 9点)',
                '晚间播报(2019年8月14日星期日 10点)',
                '晚间播报(2019年8月14日星期日 11点)',
                '晚间播报(2019年8月14日星期日 12点)',
                '晚间播报(2019年8月14日星期日 13点)',
                '晚间播报(2019年8月14日星期日 14点)',
            ],
            ind: 0
        };
    },
    components: {
        
    },
    computed: {
        
    },
    methods: {
        changeBgc: function (index) {
            this.ind = index;
            this.$router.push({
                path:
                    "/eveningBroadcasting/" +
                    this.todaytime +
                    "/" +
                    this.userid +
                    "/" +
                    index +
                    "/" +
                    this.openid +
                    "/" +
                    this.isShare
            });
            //使用路由刷新
		    this.routerRefresh();
        }
        
    },
    created(){
        
    },
    mounted(){
        
    },
}
</script>

法二

App.vue

<!-- App.vue根组件代码 -->
<template>
  <div class="app">
      <div class="content">
      	   <router-view :key="key"></router-view>
      </div>
  </div>
</template>
 
<script>
    export default{
	    data(){
	        return {
 
		    }
	    },
	    computed:{
	        key(){
	            return this.$route.path + Math.random();
	        }
	    }
    }
</script>

给<router-view :key="key"></router-view>增加一个不同:key值,这样vue就会识别这是不同的<router-view>了。 这时候路由就会更新了。不过这也就意味着需要把每个<router-view>都绑定一个key值。如果我从page1跳到page2不同组件的话,我其实是不用担心组件更新问题的。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值