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

Sye*_*yed 8 javascript vue.js vuex

这工作得很好。

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

},

methods: {
  ...mapActions(['shared/getPosts', 'posts/getPosts']),
},
Run Code Online (Sandbox Code Playgroud)

但是,我想知道,有没有办法让下面的代码按预期工作,请参考评论:

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

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

Max*_*huk 14

像这样重命名

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()`
  }),
},
Run Code Online (Sandbox Code Playgroud)

更多信息在这里

  • 有没有办法一次重命名多个动作?类似于:`...mapActions('shared', [{getSharedPosts: 'getPosts'}, {getSomething: 'something'}]),` (3认同)