问题
This works just fine.
created() {
this['shared/getPosts']();
this['posts/getPosts']();
},
methods: {
...mapActions(['shared/getPosts', 'posts/getPosts']),
},
But, I was wondering, is there a way to make below code work as expected, please refer comment:
created() {
this.getPosts(); // triggers last method
},
methods: {
...mapActions('shared', ['getPosts']), // how to trigger this?
...mapActions('posts', ['getPosts']), // this gets triggered.
},
回答1:
Just rename like so
created() {
// call the first method
this.getSharedPosts();
// or second
this.getPosts();
},
methods: {
...mapActions('shared', {
getSharedPosts: 'getPosts', // <-- call it as `this.getSharedPosts()` in component
}),
...mapActions('posts', {
getPosts: 'getPosts', // <-- call it as `this.getPosts()`
}),
},
More info here
来源:https://stackoverflow.com/questions/55269463/how-to-access-vue-vuex-mapactions-methods-with-similar-name-while-has-different