[Vuejs]-Vue is returning vm as undefined whenever I try to access it

1👍

Assuming search is in your methods it should not be using an arrow function as that will give you the wrong this binding.

Instead use:

methods: {
  search: throttle(function (live) {
     // ...
  }, 500)
}

Here I’m also assuming that throttle will preserve the this value, which would be typical for implementations of throttling.

0👍

Like I said in my comment, I suspect this is a scoping issue.
Perhaps if you return the throttle function with the Vue component passed in, you might see better results:

search: function() {
let vm = this;

return throttle(live => {
      console.log("entered!!!");
      console.log("this", this);
      console.log("vm", vm);

      if (typeof live == "undefined") {
        live = true;
      }

      if (!live) {
        // We are on the search page, we need to update the results
        if (vm.$route.name != "search") {
          vm.$router.push({ name: "search" });
        }
      }

      vm.$store.dispatch("search/get", {
        type: vm.searchType,
        query: vm.searchQuery
      });
    }, 500)
}
👤Tanner

Leave a comment