vue同路由下不同参数更新页面方法

我们知道,同路由下不同参数,vue会认为是一个页面不会进行刷新!

那么也就不会执行

created方法!

下面介绍2种更新方法

1、导航守卫beforeRouteUpdate  (2.2 新增)

beforeRouteUpdate (to, from, next) {
this.typeid = to.query.typeid || null // 获取id
this.routeread(this.typeid, 1, 10) // 根据this.typeid,请求不同数据
console.log(this.zoneId)
next()
},

2、 watch 监听路由变化

  watch: {
    // 监听相同路由下参数变化的时候,从而实现异步刷新
    '$route' (to, from) {
      // 做一些路由变化的响应
      // 打开加载动画
      this.loading = true
      // 重新获取数据
      // this.initData();
      /** 初始化其他数据 */
      let typeid = this.$route.query.typeid
      if (typeid) {
        this.typeid = typeid
        this.typename = this.$route.query.typename
      }
      this.initData()
    }
    count: {
handler (newName, oldName) {
// 监听总数变化执行分页函数
this.fenye()
},
immediate: false
}
  }


相关内容

发表评论

验证码:
点击我更换图片

最新评论