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?
javascript html vue.js vuejs2 vuex
add a comment |
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?
javascript html vue.js vuejs2 vuex
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 importstore
in component?
– ittus
Nov 23 at 2:16
I'm importing the store withimport store from '../store/module'
. The store is contained in the module.js file.
– mmjin
Nov 23 at 6:47
add a comment |
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?
javascript html vue.js vuejs2 vuex
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
javascript html vue.js vuejs2 vuex
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 importstore
in component?
– ittus
Nov 23 at 2:16
I'm importing the store withimport store from '../store/module'
. The store is contained in the module.js file.
– mmjin
Nov 23 at 6:47
add a comment |
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 importstore
in component?
– ittus
Nov 23 at 2:16
I'm importing the store withimport 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
add a comment |
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
}
}
}
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
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',
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%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
}
}
}
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
add a comment |
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
}
}
}
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
add a comment |
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
}
}
}
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
}
}
}
answered Nov 22 at 15:03
Austio
4,5121128
4,5121128
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
add a comment |
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
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.
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.
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%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
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
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