source

네임스페이스가 다른 유사한 이름의 vue/vuex mapActions 메서드에 액세스하는 방법

goodcode 2022. 8. 21. 14:08
반응형

네임스페이스가 다른 유사한 이름의 vue/vuex mapActions 메서드에 액세스하는 방법

이 정도면 충분합니다.

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.
},

이름만 그렇게 바꿔주세요.

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

자세한 내용은 이쪽

언급URL : https://stackoverflow.com/questions/55269463/how-to-access-vue-vuex-mapactions-methods-with-similar-name-while-has-different

반응형