Vue. Set focus on a component's input when value in Vuex store is modified
So this is the code I have in my component:
//template
<v-select ref='ItemSearchSelect' :options="options"></v-select>
...
//script
created: function () {
this.$store.subscribe((setFocusSearch, state) => {
if (setFocusSearch.type == 'setFocusSearch' && setFocusSearch.payload == true){
this.$refs.ItemSearchSelect.$refs.search.focus()
this.$store.commit('setFocusSearch',false)
}
})
},
This is my store mutation, which can be called from any other component:
setFocusSearch(state,val){
state.focussearch = val;
},
Sometimes it's working correctly, but other times I can see this error in console, and the focus is not working:
Uncaught TypeError: Cannot read property '$refs' of undefined
at eval (ItemSearch.vue?d78c:38)
at eval (vuex.esm.js?2f62:392)
at Array.forEach (<anonymous>)
at Store.commit (vuex.esm.js?2f62:392)
at Store.boundCommit [as commit] (vuex.esm.js?2f62:335)
at VueComponent.focusSearch (ShoppingCart.vue?0f5b:96)
at keydown (eval at ./node_modules/cache-loader/dist/cjs.js?
...
It always happens in this case:
Router push to a a view with my ItemSearch component and the component that triggers the store mutation. At this moment everything is working fine. Now I router push to another view that has nothing to do, and then I go back with router push to the original view. At this moment, when the mutation is triggered, I get the error.
Any ideas on what's going on?
javascript vue.js vuex
add a comment |
So this is the code I have in my component:
//template
<v-select ref='ItemSearchSelect' :options="options"></v-select>
...
//script
created: function () {
this.$store.subscribe((setFocusSearch, state) => {
if (setFocusSearch.type == 'setFocusSearch' && setFocusSearch.payload == true){
this.$refs.ItemSearchSelect.$refs.search.focus()
this.$store.commit('setFocusSearch',false)
}
})
},
This is my store mutation, which can be called from any other component:
setFocusSearch(state,val){
state.focussearch = val;
},
Sometimes it's working correctly, but other times I can see this error in console, and the focus is not working:
Uncaught TypeError: Cannot read property '$refs' of undefined
at eval (ItemSearch.vue?d78c:38)
at eval (vuex.esm.js?2f62:392)
at Array.forEach (<anonymous>)
at Store.commit (vuex.esm.js?2f62:392)
at Store.boundCommit [as commit] (vuex.esm.js?2f62:335)
at VueComponent.focusSearch (ShoppingCart.vue?0f5b:96)
at keydown (eval at ./node_modules/cache-loader/dist/cjs.js?
...
It always happens in this case:
Router push to a a view with my ItemSearch component and the component that triggers the store mutation. At this moment everything is working fine. Now I router push to another view that has nothing to do, and then I go back with router push to the original view. At this moment, when the mutation is triggered, I get the error.
Any ideas on what's going on?
javascript vue.js vuex
add a comment |
So this is the code I have in my component:
//template
<v-select ref='ItemSearchSelect' :options="options"></v-select>
...
//script
created: function () {
this.$store.subscribe((setFocusSearch, state) => {
if (setFocusSearch.type == 'setFocusSearch' && setFocusSearch.payload == true){
this.$refs.ItemSearchSelect.$refs.search.focus()
this.$store.commit('setFocusSearch',false)
}
})
},
This is my store mutation, which can be called from any other component:
setFocusSearch(state,val){
state.focussearch = val;
},
Sometimes it's working correctly, but other times I can see this error in console, and the focus is not working:
Uncaught TypeError: Cannot read property '$refs' of undefined
at eval (ItemSearch.vue?d78c:38)
at eval (vuex.esm.js?2f62:392)
at Array.forEach (<anonymous>)
at Store.commit (vuex.esm.js?2f62:392)
at Store.boundCommit [as commit] (vuex.esm.js?2f62:335)
at VueComponent.focusSearch (ShoppingCart.vue?0f5b:96)
at keydown (eval at ./node_modules/cache-loader/dist/cjs.js?
...
It always happens in this case:
Router push to a a view with my ItemSearch component and the component that triggers the store mutation. At this moment everything is working fine. Now I router push to another view that has nothing to do, and then I go back with router push to the original view. At this moment, when the mutation is triggered, I get the error.
Any ideas on what's going on?
javascript vue.js vuex
So this is the code I have in my component:
//template
<v-select ref='ItemSearchSelect' :options="options"></v-select>
...
//script
created: function () {
this.$store.subscribe((setFocusSearch, state) => {
if (setFocusSearch.type == 'setFocusSearch' && setFocusSearch.payload == true){
this.$refs.ItemSearchSelect.$refs.search.focus()
this.$store.commit('setFocusSearch',false)
}
})
},
This is my store mutation, which can be called from any other component:
setFocusSearch(state,val){
state.focussearch = val;
},
Sometimes it's working correctly, but other times I can see this error in console, and the focus is not working:
Uncaught TypeError: Cannot read property '$refs' of undefined
at eval (ItemSearch.vue?d78c:38)
at eval (vuex.esm.js?2f62:392)
at Array.forEach (<anonymous>)
at Store.commit (vuex.esm.js?2f62:392)
at Store.boundCommit [as commit] (vuex.esm.js?2f62:335)
at VueComponent.focusSearch (ShoppingCart.vue?0f5b:96)
at keydown (eval at ./node_modules/cache-loader/dist/cjs.js?
...
It always happens in this case:
Router push to a a view with my ItemSearch component and the component that triggers the store mutation. At this moment everything is working fine. Now I router push to another view that has nothing to do, and then I go back with router push to the original view. At this moment, when the mutation is triggered, I get the error.
Any ideas on what's going on?
javascript vue.js vuex
javascript vue.js vuex
asked Jan 2 at 0:06
Jack CasasJack Casas
316619
316619
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When your component is unmounted / destroyed, the mutation subscription will live on in the store. This is why the this
reference in your subscriber becomes undefined
.
What you need to do is remove the subscription when your component is destroyed. See https://vuex.vuejs.org/api/#subscribe...
To stop subscribing, call the returned unsubscribe function.
For example
mounted () {
// the name isn't really important and since this isn't a reactive property,
// you don't need to have this defined in "data"
this.focusSearchUnsubscriber = this.$store.subscribe(...)
},
beforeDestroy () {
this.focusSearchUnsubscriber() // call the unsubscribe function
}
You'll note I've also used mounted
instead of created
. This is because your $refs
won't be populated until your component is mounted so using the later lifecycle hook adds a bit of extra safety.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999936%2fvue-set-focus-on-a-components-input-when-value-in-vuex-store-is-modified%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When your component is unmounted / destroyed, the mutation subscription will live on in the store. This is why the this
reference in your subscriber becomes undefined
.
What you need to do is remove the subscription when your component is destroyed. See https://vuex.vuejs.org/api/#subscribe...
To stop subscribing, call the returned unsubscribe function.
For example
mounted () {
// the name isn't really important and since this isn't a reactive property,
// you don't need to have this defined in "data"
this.focusSearchUnsubscriber = this.$store.subscribe(...)
},
beforeDestroy () {
this.focusSearchUnsubscriber() // call the unsubscribe function
}
You'll note I've also used mounted
instead of created
. This is because your $refs
won't be populated until your component is mounted so using the later lifecycle hook adds a bit of extra safety.
add a comment |
When your component is unmounted / destroyed, the mutation subscription will live on in the store. This is why the this
reference in your subscriber becomes undefined
.
What you need to do is remove the subscription when your component is destroyed. See https://vuex.vuejs.org/api/#subscribe...
To stop subscribing, call the returned unsubscribe function.
For example
mounted () {
// the name isn't really important and since this isn't a reactive property,
// you don't need to have this defined in "data"
this.focusSearchUnsubscriber = this.$store.subscribe(...)
},
beforeDestroy () {
this.focusSearchUnsubscriber() // call the unsubscribe function
}
You'll note I've also used mounted
instead of created
. This is because your $refs
won't be populated until your component is mounted so using the later lifecycle hook adds a bit of extra safety.
add a comment |
When your component is unmounted / destroyed, the mutation subscription will live on in the store. This is why the this
reference in your subscriber becomes undefined
.
What you need to do is remove the subscription when your component is destroyed. See https://vuex.vuejs.org/api/#subscribe...
To stop subscribing, call the returned unsubscribe function.
For example
mounted () {
// the name isn't really important and since this isn't a reactive property,
// you don't need to have this defined in "data"
this.focusSearchUnsubscriber = this.$store.subscribe(...)
},
beforeDestroy () {
this.focusSearchUnsubscriber() // call the unsubscribe function
}
You'll note I've also used mounted
instead of created
. This is because your $refs
won't be populated until your component is mounted so using the later lifecycle hook adds a bit of extra safety.
When your component is unmounted / destroyed, the mutation subscription will live on in the store. This is why the this
reference in your subscriber becomes undefined
.
What you need to do is remove the subscription when your component is destroyed. See https://vuex.vuejs.org/api/#subscribe...
To stop subscribing, call the returned unsubscribe function.
For example
mounted () {
// the name isn't really important and since this isn't a reactive property,
// you don't need to have this defined in "data"
this.focusSearchUnsubscriber = this.$store.subscribe(...)
},
beforeDestroy () {
this.focusSearchUnsubscriber() // call the unsubscribe function
}
You'll note I've also used mounted
instead of created
. This is because your $refs
won't be populated until your component is mounted so using the later lifecycle hook adds a bit of extra safety.
edited Jan 2 at 0:23


Boussadjra Brahim
8,1843733
8,1843733
answered Jan 2 at 0:17
PhilPhil
99.1k12145163
99.1k12145163
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999936%2fvue-set-focus-on-a-components-input-when-value-in-vuex-store-is-modified%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown