在具有不同名称空间的情况下,如何访问名称相似的vue / vuex mapActions方法?

时间:2019-03-20 20:15:00

标签: javascript vue.js vuex

这很好用。

created() {
  this['shared/getPosts']();
  this['posts/getPosts']();

},

methods: {
  ...mapActions(['shared/getPosts', 'posts/getPosts']),
},

但是,我想知道,是否有一种方法可以使下面的代码按预期工作,请参阅注释:

created() {
  this.getPosts(); // triggers last method
},

methods: {
  ...mapActions('shared', ['getPosts']), // how to trigger this?
  ...mapActions('posts', ['getPosts']), // this gets triggered.
},

1 个答案:

答案 0 :(得分:3)

只是这样重命名

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()`
  }),
},

更多信息here