8

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.
},
Syed
  • 15,657
  • 13
  • 120
  • 154

1 Answers1

14

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

Max Liashuk
  • 1,043
  • 7
  • 18
  • 3
    Is there no way to rename multiple actions at once? something like: `...mapActions('shared', [{getSharedPosts: 'getPosts'}, {getSomething: 'something'}]),` – Shaya Ulman Sep 22 '20 at 16:58
  • @ShayaUlman: just pass in only one object `...mapActions('shared', { getSharedPosts: 'getPosts', getPosts: 'getPosts' })` – bonniss Apr 13 '21 at 15:17