简体   繁体   English

Static vue过滤数组在我需要的时候是空的

[英]Static vue filtered Array is empty when I need it

I have a template that needs some non-reactive data in it from my Vuex store.我有一个模板,它需要我的 Vuex 商店中的一些非反应性数据。 However at the moment, I have to manually switch views to get the data to load.但是目前,我必须手动切换视图才能加载数据。 I am assuming I should not use mounted or created .我假设我不应该使用mountedcreated If I use watch , then it basically becomes reactive again, and I only want to get this once.如果我使用watch ,那么它基本上会再次变得被动,我只想得到这个。

 data: function () {
    return {
      localreadmode: false,
      myArray: null,
    }
  },

  computed: {
    ...mapState({
      myNodes: (state) => state.myNodes,
      configPositions: (state) => state.configPositions,
      configEmoji: (state) => state.configEmoji,
    }),

    nodes_filtered: function () {
      return this.myNodes.filter((nodes) => {
        return nodes.deleted == false
      })
    },
  },

  // this is to stop sync chasing bug
  myArray: null,
  created() {
    this.$options.myArray = this.nodes_filtered
   console.log(this.nodes_filtered)
// is empty unless I switch views
  },

You could still use a watcher that runs only once via the vm.$watch API.您仍然可以通过vm.$watch API 使用仅运行一次的观察程序。 It returns a method that can be called to stop watching the value, so your handler could invoke it when nodes_filtered[] is not empty.它返回一个可以被调用来停止观察值的方法,因此您的处理程序可以在nodes_filtered[]不为空时调用它。

export default {
  mounted() {
    const unwatch = this.$watch(this.nodes_filtered, value => {
      // ignore falsy values
      if (!value) return

      // stop watching when nodes_filtered[] is not empty
      if (value.length) unwatch()

      // process value here
    })
  }
}

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM