1

I have a template that needs some non-reactive data in it from my Vuex store. 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. If I use watch, then it basically becomes reactive again, and I only want to get this once.

 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
  },
3
  • Don't use variables outside data() function Commented Dec 7, 2020 at 17:31
  • Why can't you make data in vuex store reactive? It's the exact way all data in such store should be. Commented Dec 7, 2020 at 17:39
  • @anatoly the app is more complicate but I have realtime sync in action and so when you are editing your own text you dont want this on as you cant edit your text - you end up in a race with sync Commented Dec 7, 2020 at 17:41

1 Answer 1

1

You could still use a watcher that runs only once via the 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.

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks I did a hack with some timers, but this is not good obviously as would be caught out by hardware / internet speed I suspect... but I will try this instead seems much better
I had to add this.$forceUpdate() into the unwatch but this has reduced some of the complications with timers - thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.