Vuex action is not recognized as a function inside component's data()
I am having an issue with triggering a method in the vue component. I am using Element UI with Vuex, and would like to trigger a state change (make another component visible) when a shortcut in the datetimepicker element is clicked. Here's the component code:
<template>
<div id="DateTimePicker">
<div/>
TimeInterval state: {{this.$store.getters.timeInterval}}
<div/>
<el-date-picker
:value="this.timeInterval"
@input="this.setTimeInterval"
type="datetimerange"
:picker-options="dateTimeShortcuts"
range-separator="To"
format="yyyy-MM-dd HH:mm"
editable
time-arrow-control
start-placeholder="Start date"
end-placeholder="End date"
align="right"
size="large">
</el-date-picker>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
name: 'DateTimePicker',
computed: {
...mapGetters(['timeInterval']),
},
methods: {
...mapActions(['setTimeInterval', 'setRefreshRateVisibility']),
},
data() {
return {
dateTimeShortcuts: {
disabledDate(date) {
return date > new Date() || date < new Date('12/1/18');
},
shortcuts: [
{
text: '15 Minutes',
onClick(picker) {
this.setRefreshRateVisibility(true); // Vue does not recognize this as a function
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 900 * 1000);
picker.$emit('pick', [start, end]);
},
},
],
},
};
},
};
</script>
When I click on the shortcut - I get an error:
Uncaught TypeError: this.setRefreshRateVisibility is not a function
When I do
<div id="DateTimePicker" @click="this.setRefreshRateVisibility(true)">
it works fine, so the problem is not with the action itself, but with how I invoke this action, I think. Here's the store file:
export default {
state: {
isRefreshRateVisible: false,
},
mutations: {
SET_VISIBILITY(state, newValue) {
state.isRefreshRateVisible = newValue;
},
},
actions: {
setRefreshRateVisibility: ({ commit }, newValue) => {
commit('SET_VISIBILITY', newValue);
},
},
getters: {
isRefreshRateVisible: state => state.isRefreshRateVisible,
},
};
Can anybody help me resole this, please?
vue.js vuex
add a comment |
I am having an issue with triggering a method in the vue component. I am using Element UI with Vuex, and would like to trigger a state change (make another component visible) when a shortcut in the datetimepicker element is clicked. Here's the component code:
<template>
<div id="DateTimePicker">
<div/>
TimeInterval state: {{this.$store.getters.timeInterval}}
<div/>
<el-date-picker
:value="this.timeInterval"
@input="this.setTimeInterval"
type="datetimerange"
:picker-options="dateTimeShortcuts"
range-separator="To"
format="yyyy-MM-dd HH:mm"
editable
time-arrow-control
start-placeholder="Start date"
end-placeholder="End date"
align="right"
size="large">
</el-date-picker>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
name: 'DateTimePicker',
computed: {
...mapGetters(['timeInterval']),
},
methods: {
...mapActions(['setTimeInterval', 'setRefreshRateVisibility']),
},
data() {
return {
dateTimeShortcuts: {
disabledDate(date) {
return date > new Date() || date < new Date('12/1/18');
},
shortcuts: [
{
text: '15 Minutes',
onClick(picker) {
this.setRefreshRateVisibility(true); // Vue does not recognize this as a function
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 900 * 1000);
picker.$emit('pick', [start, end]);
},
},
],
},
};
},
};
</script>
When I click on the shortcut - I get an error:
Uncaught TypeError: this.setRefreshRateVisibility is not a function
When I do
<div id="DateTimePicker" @click="this.setRefreshRateVisibility(true)">
it works fine, so the problem is not with the action itself, but with how I invoke this action, I think. Here's the store file:
export default {
state: {
isRefreshRateVisible: false,
},
mutations: {
SET_VISIBILITY(state, newValue) {
state.isRefreshRateVisible = newValue;
},
},
actions: {
setRefreshRateVisibility: ({ commit }, newValue) => {
commit('SET_VISIBILITY', newValue);
},
},
getters: {
isRefreshRateVisible: state => state.isRefreshRateVisible,
},
};
Can anybody help me resole this, please?
vue.js vuex
add a comment |
I am having an issue with triggering a method in the vue component. I am using Element UI with Vuex, and would like to trigger a state change (make another component visible) when a shortcut in the datetimepicker element is clicked. Here's the component code:
<template>
<div id="DateTimePicker">
<div/>
TimeInterval state: {{this.$store.getters.timeInterval}}
<div/>
<el-date-picker
:value="this.timeInterval"
@input="this.setTimeInterval"
type="datetimerange"
:picker-options="dateTimeShortcuts"
range-separator="To"
format="yyyy-MM-dd HH:mm"
editable
time-arrow-control
start-placeholder="Start date"
end-placeholder="End date"
align="right"
size="large">
</el-date-picker>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
name: 'DateTimePicker',
computed: {
...mapGetters(['timeInterval']),
},
methods: {
...mapActions(['setTimeInterval', 'setRefreshRateVisibility']),
},
data() {
return {
dateTimeShortcuts: {
disabledDate(date) {
return date > new Date() || date < new Date('12/1/18');
},
shortcuts: [
{
text: '15 Minutes',
onClick(picker) {
this.setRefreshRateVisibility(true); // Vue does not recognize this as a function
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 900 * 1000);
picker.$emit('pick', [start, end]);
},
},
],
},
};
},
};
</script>
When I click on the shortcut - I get an error:
Uncaught TypeError: this.setRefreshRateVisibility is not a function
When I do
<div id="DateTimePicker" @click="this.setRefreshRateVisibility(true)">
it works fine, so the problem is not with the action itself, but with how I invoke this action, I think. Here's the store file:
export default {
state: {
isRefreshRateVisible: false,
},
mutations: {
SET_VISIBILITY(state, newValue) {
state.isRefreshRateVisible = newValue;
},
},
actions: {
setRefreshRateVisibility: ({ commit }, newValue) => {
commit('SET_VISIBILITY', newValue);
},
},
getters: {
isRefreshRateVisible: state => state.isRefreshRateVisible,
},
};
Can anybody help me resole this, please?
vue.js vuex
I am having an issue with triggering a method in the vue component. I am using Element UI with Vuex, and would like to trigger a state change (make another component visible) when a shortcut in the datetimepicker element is clicked. Here's the component code:
<template>
<div id="DateTimePicker">
<div/>
TimeInterval state: {{this.$store.getters.timeInterval}}
<div/>
<el-date-picker
:value="this.timeInterval"
@input="this.setTimeInterval"
type="datetimerange"
:picker-options="dateTimeShortcuts"
range-separator="To"
format="yyyy-MM-dd HH:mm"
editable
time-arrow-control
start-placeholder="Start date"
end-placeholder="End date"
align="right"
size="large">
</el-date-picker>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
name: 'DateTimePicker',
computed: {
...mapGetters(['timeInterval']),
},
methods: {
...mapActions(['setTimeInterval', 'setRefreshRateVisibility']),
},
data() {
return {
dateTimeShortcuts: {
disabledDate(date) {
return date > new Date() || date < new Date('12/1/18');
},
shortcuts: [
{
text: '15 Minutes',
onClick(picker) {
this.setRefreshRateVisibility(true); // Vue does not recognize this as a function
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 900 * 1000);
picker.$emit('pick', [start, end]);
},
},
],
},
};
},
};
</script>
When I click on the shortcut - I get an error:
Uncaught TypeError: this.setRefreshRateVisibility is not a function
When I do
<div id="DateTimePicker" @click="this.setRefreshRateVisibility(true)">
it works fine, so the problem is not with the action itself, but with how I invoke this action, I think. Here's the store file:
export default {
state: {
isRefreshRateVisible: false,
},
mutations: {
SET_VISIBILITY(state, newValue) {
state.isRefreshRateVisible = newValue;
},
},
actions: {
setRefreshRateVisibility: ({ commit }, newValue) => {
commit('SET_VISIBILITY', newValue);
},
},
getters: {
isRefreshRateVisible: state => state.isRefreshRateVisible,
},
};
Can anybody help me resole this, please?
vue.js vuex
vue.js vuex
asked Jan 3 at 10:22
VItalii LiashchenkoVItalii Liashchenko
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You possibly seem to have a wrong reference to this
. In JavaScript context, this
refers to the immediate context, but Vue functions are tied to the context of the class/component.
To prevent this, copy the reference of this to another variable.
data(){
let componentObj = this;
...
And then call the method using that reference.
onClick(picker) {
componentObj.setRefreshRateVisibility(true);
Thank you! That did help. Is this the "canonical" way to circumvent this? I can't find any directions in the documentation.
– VItalii Liashchenko
Jan 3 at 11:09
You can use an arrow function.Onclick: (picker) => { ... }
– Frank
Jan 3 at 11:31
Like @Frank said, arrow functions dont change scope like other functions do.
– uniquerockrz
Jan 3 at 12:12
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%2f54020338%2fvuex-action-is-not-recognized-as-a-function-inside-components-data%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
You possibly seem to have a wrong reference to this
. In JavaScript context, this
refers to the immediate context, but Vue functions are tied to the context of the class/component.
To prevent this, copy the reference of this to another variable.
data(){
let componentObj = this;
...
And then call the method using that reference.
onClick(picker) {
componentObj.setRefreshRateVisibility(true);
Thank you! That did help. Is this the "canonical" way to circumvent this? I can't find any directions in the documentation.
– VItalii Liashchenko
Jan 3 at 11:09
You can use an arrow function.Onclick: (picker) => { ... }
– Frank
Jan 3 at 11:31
Like @Frank said, arrow functions dont change scope like other functions do.
– uniquerockrz
Jan 3 at 12:12
add a comment |
You possibly seem to have a wrong reference to this
. In JavaScript context, this
refers to the immediate context, but Vue functions are tied to the context of the class/component.
To prevent this, copy the reference of this to another variable.
data(){
let componentObj = this;
...
And then call the method using that reference.
onClick(picker) {
componentObj.setRefreshRateVisibility(true);
Thank you! That did help. Is this the "canonical" way to circumvent this? I can't find any directions in the documentation.
– VItalii Liashchenko
Jan 3 at 11:09
You can use an arrow function.Onclick: (picker) => { ... }
– Frank
Jan 3 at 11:31
Like @Frank said, arrow functions dont change scope like other functions do.
– uniquerockrz
Jan 3 at 12:12
add a comment |
You possibly seem to have a wrong reference to this
. In JavaScript context, this
refers to the immediate context, but Vue functions are tied to the context of the class/component.
To prevent this, copy the reference of this to another variable.
data(){
let componentObj = this;
...
And then call the method using that reference.
onClick(picker) {
componentObj.setRefreshRateVisibility(true);
You possibly seem to have a wrong reference to this
. In JavaScript context, this
refers to the immediate context, but Vue functions are tied to the context of the class/component.
To prevent this, copy the reference of this to another variable.
data(){
let componentObj = this;
...
And then call the method using that reference.
onClick(picker) {
componentObj.setRefreshRateVisibility(true);
answered Jan 3 at 10:35
uniquerockrzuniquerockrz
20629
20629
Thank you! That did help. Is this the "canonical" way to circumvent this? I can't find any directions in the documentation.
– VItalii Liashchenko
Jan 3 at 11:09
You can use an arrow function.Onclick: (picker) => { ... }
– Frank
Jan 3 at 11:31
Like @Frank said, arrow functions dont change scope like other functions do.
– uniquerockrz
Jan 3 at 12:12
add a comment |
Thank you! That did help. Is this the "canonical" way to circumvent this? I can't find any directions in the documentation.
– VItalii Liashchenko
Jan 3 at 11:09
You can use an arrow function.Onclick: (picker) => { ... }
– Frank
Jan 3 at 11:31
Like @Frank said, arrow functions dont change scope like other functions do.
– uniquerockrz
Jan 3 at 12:12
Thank you! That did help. Is this the "canonical" way to circumvent this? I can't find any directions in the documentation.
– VItalii Liashchenko
Jan 3 at 11:09
Thank you! That did help. Is this the "canonical" way to circumvent this? I can't find any directions in the documentation.
– VItalii Liashchenko
Jan 3 at 11:09
You can use an arrow function.
Onclick: (picker) => { ... }
– Frank
Jan 3 at 11:31
You can use an arrow function.
Onclick: (picker) => { ... }
– Frank
Jan 3 at 11:31
Like @Frank said, arrow functions dont change scope like other functions do.
– uniquerockrz
Jan 3 at 12:12
Like @Frank said, arrow functions dont change scope like other functions do.
– uniquerockrz
Jan 3 at 12:12
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%2f54020338%2fvuex-action-is-not-recognized-as-a-function-inside-components-data%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