Vue.js/Vuex: How do I v-bind to a state value?











up vote
2
down vote

favorite












I'm trying to bind the text in a b-dropdown element to a value in the store. I tried binding to a computed property since the value in the store can change and the b-dropdown's text should change dynamically to reflect this change. I want to store the value in the store rather than as a data object because the value has to persist outside of the component where the b-dropdown exists.



Here's the b-dropdown element:



<b-dropdown v-bind:text="selectedSearchType" variant="outline-secondary">
...
</b-dropdown>


And the computed property



computed: {
selectedSearchType: function() {
return store.getters.getSelectedSearchType
}
},


The getter



getSelectedSearchType: state => {
return state.selectedSearchType
}


The state



state: {
selectedSearchType: "Item",
.....
}


I'm getting the following error:



[Vue warn]: Invalid prop: type check failed for prop "text". Expected String, got Function.


Instead if I do



<b-dropdown v-bind:text="selectedSearchType()" variant="outline-secondary">


I get



[Vue warn]: Error in render: "TypeError: Cannot read property 'selectedSearchType' of undefined"


How do I fix this to make the b-dropdown's text bind to the selectedSearchType ins the store?










share|improve this question
























  • Would you mind to share the getSelectedSearchType getter with us?
    – Dawid Zbiński
    Nov 22 at 14:09










  • @DawidZbiński, I've edited my question to include the getter and the state
    – mmjin
    Nov 22 at 14:42










  • How do you import store in component?
    – ittus
    Nov 23 at 2:16










  • I'm importing the store with import store from '../store/module'. The store is contained in the module.js file.
    – mmjin
    Nov 23 at 6:47















up vote
2
down vote

favorite












I'm trying to bind the text in a b-dropdown element to a value in the store. I tried binding to a computed property since the value in the store can change and the b-dropdown's text should change dynamically to reflect this change. I want to store the value in the store rather than as a data object because the value has to persist outside of the component where the b-dropdown exists.



Here's the b-dropdown element:



<b-dropdown v-bind:text="selectedSearchType" variant="outline-secondary">
...
</b-dropdown>


And the computed property



computed: {
selectedSearchType: function() {
return store.getters.getSelectedSearchType
}
},


The getter



getSelectedSearchType: state => {
return state.selectedSearchType
}


The state



state: {
selectedSearchType: "Item",
.....
}


I'm getting the following error:



[Vue warn]: Invalid prop: type check failed for prop "text". Expected String, got Function.


Instead if I do



<b-dropdown v-bind:text="selectedSearchType()" variant="outline-secondary">


I get



[Vue warn]: Error in render: "TypeError: Cannot read property 'selectedSearchType' of undefined"


How do I fix this to make the b-dropdown's text bind to the selectedSearchType ins the store?










share|improve this question
























  • Would you mind to share the getSelectedSearchType getter with us?
    – Dawid Zbiński
    Nov 22 at 14:09










  • @DawidZbiński, I've edited my question to include the getter and the state
    – mmjin
    Nov 22 at 14:42










  • How do you import store in component?
    – ittus
    Nov 23 at 2:16










  • I'm importing the store with import store from '../store/module'. The store is contained in the module.js file.
    – mmjin
    Nov 23 at 6:47













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to bind the text in a b-dropdown element to a value in the store. I tried binding to a computed property since the value in the store can change and the b-dropdown's text should change dynamically to reflect this change. I want to store the value in the store rather than as a data object because the value has to persist outside of the component where the b-dropdown exists.



Here's the b-dropdown element:



<b-dropdown v-bind:text="selectedSearchType" variant="outline-secondary">
...
</b-dropdown>


And the computed property



computed: {
selectedSearchType: function() {
return store.getters.getSelectedSearchType
}
},


The getter



getSelectedSearchType: state => {
return state.selectedSearchType
}


The state



state: {
selectedSearchType: "Item",
.....
}


I'm getting the following error:



[Vue warn]: Invalid prop: type check failed for prop "text". Expected String, got Function.


Instead if I do



<b-dropdown v-bind:text="selectedSearchType()" variant="outline-secondary">


I get



[Vue warn]: Error in render: "TypeError: Cannot read property 'selectedSearchType' of undefined"


How do I fix this to make the b-dropdown's text bind to the selectedSearchType ins the store?










share|improve this question















I'm trying to bind the text in a b-dropdown element to a value in the store. I tried binding to a computed property since the value in the store can change and the b-dropdown's text should change dynamically to reflect this change. I want to store the value in the store rather than as a data object because the value has to persist outside of the component where the b-dropdown exists.



Here's the b-dropdown element:



<b-dropdown v-bind:text="selectedSearchType" variant="outline-secondary">
...
</b-dropdown>


And the computed property



computed: {
selectedSearchType: function() {
return store.getters.getSelectedSearchType
}
},


The getter



getSelectedSearchType: state => {
return state.selectedSearchType
}


The state



state: {
selectedSearchType: "Item",
.....
}


I'm getting the following error:



[Vue warn]: Invalid prop: type check failed for prop "text". Expected String, got Function.


Instead if I do



<b-dropdown v-bind:text="selectedSearchType()" variant="outline-secondary">


I get



[Vue warn]: Error in render: "TypeError: Cannot read property 'selectedSearchType' of undefined"


How do I fix this to make the b-dropdown's text bind to the selectedSearchType ins the store?







javascript html vue.js vuejs2 vuex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 14:41

























asked Nov 22 at 13:48









mmjin

112




112












  • Would you mind to share the getSelectedSearchType getter with us?
    – Dawid Zbiński
    Nov 22 at 14:09










  • @DawidZbiński, I've edited my question to include the getter and the state
    – mmjin
    Nov 22 at 14:42










  • How do you import store in component?
    – ittus
    Nov 23 at 2:16










  • I'm importing the store with import store from '../store/module'. The store is contained in the module.js file.
    – mmjin
    Nov 23 at 6:47


















  • Would you mind to share the getSelectedSearchType getter with us?
    – Dawid Zbiński
    Nov 22 at 14:09










  • @DawidZbiński, I've edited my question to include the getter and the state
    – mmjin
    Nov 22 at 14:42










  • How do you import store in component?
    – ittus
    Nov 23 at 2:16










  • I'm importing the store with import store from '../store/module'. The store is contained in the module.js file.
    – mmjin
    Nov 23 at 6:47
















Would you mind to share the getSelectedSearchType getter with us?
– Dawid Zbiński
Nov 22 at 14:09




Would you mind to share the getSelectedSearchType getter with us?
– Dawid Zbiński
Nov 22 at 14:09












@DawidZbiński, I've edited my question to include the getter and the state
– mmjin
Nov 22 at 14:42




@DawidZbiński, I've edited my question to include the getter and the state
– mmjin
Nov 22 at 14:42












How do you import store in component?
– ittus
Nov 23 at 2:16




How do you import store in component?
– ittus
Nov 23 at 2:16












I'm importing the store with import store from '../store/module'. The store is contained in the module.js file.
– mmjin
Nov 23 at 6:47




I'm importing the store with import store from '../store/module'. The store is contained in the module.js file.
– mmjin
Nov 23 at 6:47












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Your best bet on binding to the store is to create a computed getter/setter and then use v-model in your input. That would look something like the following, you will have to adjust a little depending on your values.



This also assumes that b-dropdown is going to emit an input when the value changes.



<b-dropdown v-model="selectedSearchType" variant="outline-secondary">

computed: {
selectedSearchType: {
get() {
return //value from store
},
set(val) {
// set the value in the store
}
}
}





share|improve this answer





















  • I tried what you recommended, but the text of b-dropdown was not displaying the string stored in selectedSearchType. The dropdown just had an arrow, meaning there was no specified text.
    – mmjin
    Nov 23 at 6:46










  • Hmm, on it check what the value is being passed in the vue tools in chrome. Probably the get function is not returning a value. I am confident in this approach b/c i use it often when mapping to and from the store like this.
    – Austio
    Nov 23 at 20:25











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432436%2fvue-js-vuex-how-do-i-v-bind-to-a-state-value%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








up vote
0
down vote













Your best bet on binding to the store is to create a computed getter/setter and then use v-model in your input. That would look something like the following, you will have to adjust a little depending on your values.



This also assumes that b-dropdown is going to emit an input when the value changes.



<b-dropdown v-model="selectedSearchType" variant="outline-secondary">

computed: {
selectedSearchType: {
get() {
return //value from store
},
set(val) {
// set the value in the store
}
}
}





share|improve this answer





















  • I tried what you recommended, but the text of b-dropdown was not displaying the string stored in selectedSearchType. The dropdown just had an arrow, meaning there was no specified text.
    – mmjin
    Nov 23 at 6:46










  • Hmm, on it check what the value is being passed in the vue tools in chrome. Probably the get function is not returning a value. I am confident in this approach b/c i use it often when mapping to and from the store like this.
    – Austio
    Nov 23 at 20:25















up vote
0
down vote













Your best bet on binding to the store is to create a computed getter/setter and then use v-model in your input. That would look something like the following, you will have to adjust a little depending on your values.



This also assumes that b-dropdown is going to emit an input when the value changes.



<b-dropdown v-model="selectedSearchType" variant="outline-secondary">

computed: {
selectedSearchType: {
get() {
return //value from store
},
set(val) {
// set the value in the store
}
}
}





share|improve this answer





















  • I tried what you recommended, but the text of b-dropdown was not displaying the string stored in selectedSearchType. The dropdown just had an arrow, meaning there was no specified text.
    – mmjin
    Nov 23 at 6:46










  • Hmm, on it check what the value is being passed in the vue tools in chrome. Probably the get function is not returning a value. I am confident in this approach b/c i use it often when mapping to and from the store like this.
    – Austio
    Nov 23 at 20:25













up vote
0
down vote










up vote
0
down vote









Your best bet on binding to the store is to create a computed getter/setter and then use v-model in your input. That would look something like the following, you will have to adjust a little depending on your values.



This also assumes that b-dropdown is going to emit an input when the value changes.



<b-dropdown v-model="selectedSearchType" variant="outline-secondary">

computed: {
selectedSearchType: {
get() {
return //value from store
},
set(val) {
// set the value in the store
}
}
}





share|improve this answer












Your best bet on binding to the store is to create a computed getter/setter and then use v-model in your input. That would look something like the following, you will have to adjust a little depending on your values.



This also assumes that b-dropdown is going to emit an input when the value changes.



<b-dropdown v-model="selectedSearchType" variant="outline-secondary">

computed: {
selectedSearchType: {
get() {
return //value from store
},
set(val) {
// set the value in the store
}
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 15:03









Austio

4,4971128




4,4971128












  • I tried what you recommended, but the text of b-dropdown was not displaying the string stored in selectedSearchType. The dropdown just had an arrow, meaning there was no specified text.
    – mmjin
    Nov 23 at 6:46










  • Hmm, on it check what the value is being passed in the vue tools in chrome. Probably the get function is not returning a value. I am confident in this approach b/c i use it often when mapping to and from the store like this.
    – Austio
    Nov 23 at 20:25


















  • I tried what you recommended, but the text of b-dropdown was not displaying the string stored in selectedSearchType. The dropdown just had an arrow, meaning there was no specified text.
    – mmjin
    Nov 23 at 6:46










  • Hmm, on it check what the value is being passed in the vue tools in chrome. Probably the get function is not returning a value. I am confident in this approach b/c i use it often when mapping to and from the store like this.
    – Austio
    Nov 23 at 20:25
















I tried what you recommended, but the text of b-dropdown was not displaying the string stored in selectedSearchType. The dropdown just had an arrow, meaning there was no specified text.
– mmjin
Nov 23 at 6:46




I tried what you recommended, but the text of b-dropdown was not displaying the string stored in selectedSearchType. The dropdown just had an arrow, meaning there was no specified text.
– mmjin
Nov 23 at 6:46












Hmm, on it check what the value is being passed in the vue tools in chrome. Probably the get function is not returning a value. I am confident in this approach b/c i use it often when mapping to and from the store like this.
– Austio
Nov 23 at 20:25




Hmm, on it check what the value is being passed in the vue tools in chrome. Probably the get function is not returning a value. I am confident in this approach b/c i use it often when mapping to and from the store like this.
– Austio
Nov 23 at 20:25


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432436%2fvue-js-vuex-how-do-i-v-bind-to-a-state-value%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu