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

问题描述 投票:1回答:1

这很好用。

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.
},
javascript vue.js vuex
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.