首页 > 编程技术 > js

vue路由第二次进入页面created和mounted不执行问题及解决

发布时间:2022-12-9 16:11 作者:她和海水一样咸

vue路由第二次进入页面created和mounted不执行

因为路由中created和mounted默认会进行缓存的,除非在router.js中配置:keepAlive: false;

这样是把这个页面的路由缓存给关闭了;true为开启,false为关闭;

meta: {
  keepAlive: false
},

还有一种办法就是使用activated钩子就可以了

1、将要处理的方法,放在activated函数中

总结

1、router-view路由跳转使用keep-alive方式

2、created和mounted只执行一次

3、使用activated钩子,每次进入页面执行一次

vue created、mounted等方法整理

export default {
     name: "draw",
     data(){      // 定义变量source        
       return {
         source:new ol.source.Vector({wrapX: false}),

       }
     },
    props:{ //接收父组件传递过来的参数
      map:{
        //type:String
      },

    },

mounted(){   //页面初始化方法
    if (map==map){

    }
    var vector = new ol.layer.Vector({
      source: this.source
    });
    this.map.addLayer(vector);

  },
  watch: {   //监听值变化:map值
    map:function () {
      console.log('3333'+this.map);
      //return this.map
      console.log('444444'+this.map);

      var vector = new ol.layer.Vector({
        source: this.source
      });
      this.map.addLayer(vector);
    }
  },
  methods:{   //监听方法  click事件等,执行drawFeatures方法
       drawFeatures:function(drawType){}
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/qq_40250507/article/details/120972656

标签:[!--infotagslink--]

您可能感兴趣的文章: