vue在computed中使用$nextTick方法无法动态更新dom

正在做自己的个人网站,需要通过url参数的变化来动态更新页面内容,根据教程写了这样的代码,但却达不到想要的效果。vue框架是1.0版本的,在项目中使用了vue-router和vue-infinite-loading

代码如下:

  computed: {
    tagId: {
      get: function () {
        return this.$route.params.tag
      },
      set: function () {
        this.list = []
        this.$nextTick(() => {
          this.$broadcast('$InfiniteLoading:reset')
        })
      }
    }
  },
  methods: {
    onInfinite () {
      this.$http.get('artical-list/tag/' + this.tagId + '/page/' + (this.list.length / 10 + 1)).then((res) => {
        if (res.data.length) {
          this.list = this.list.concat(res.data)
          this.$broadcast('$InfiniteLoading:loaded')
        } else {
          this.$broadcast('$InfiniteLoading:complete')
        }
      })
    }
  }

求解,谢谢!

附:vue-infinite-loading的文档:
https://peachscript.github.io...!/getting-started/with-filter

阅读 9.1k
2 个回答

set函数少了一个参数,这部分代码没有看出tagId有修改过

computed 没有生命周期,它会直接找 return,要是没有 return 瞬间返回 undefind,它不会等你的 nextTick 或一些计时器之后 return,要是想动态改变 DOM,就在 data 中静态化变量,变量绑定 DOM,在方法中控制这个变量。

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