在created 中 调用 this.$router.push 不生效的问题

在created 中,如果第一次载入app,就用weex的storage 获取city 这个key,如果没有就跳转到city页面去,现在的情况是 2 可以打印出来,第一次确实storage里面确实没值。 为什么_this.$router.push('/home/city') 没有生效

created() {
      var _this = this;
      storage.getItem('city',e => {
        if(e.result != "failed" ){
          _this.$store.commit('CURRENTCITY',e.data);
          console.log(1);
        }else {
          console.log(2);
          _this.$router.push('/home/city');
        }
      })
    },

并且将它不放在storage 中,直接放到created中也不生效

created() {
      this.$router.push('/home/city');
    },

但是如果做个点击事件,在事件中又是生效的

阅读 7.8k
1 个回答

试试放在 mounted 中呢,可能是界面没有渲染完成你就去push了呢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题