Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access vue/vuex mapActions methods with similar name while has different namespace?

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.
},
like image 571
Syed Avatar asked Mar 20 '19 20:03

Syed


1 Answers

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

like image 193
Max Liashuk Avatar answered Oct 11 '22 22:10

Max Liashuk