Home > OS >  Vuex: How to combine multiple mapGetters while namespaced: true
Vuex: How to combine multiple mapGetters while namespaced: true

Time:09-26

There are 3 scenarios that I would like to know How to combine 2 mapGetters in one.

Scenario 1

One with namespaced: true, one without

...mapGetters('cart', ['quantity']),
...mapGetters({
    isLoggedIn: 'isAuthenticated'
})

Scenario 2

Both with namespaced: true

...mapGetters('cart', ['quantity']),
...mapGetters('cart', ['totalSum'])

Scenario 3

Both with namespaced: true, but each from different module

...mapGetters('cart', ['quantity']),
...mapGetters('prods', ['products'])

CodePudding user response:

You can access namespaces like namespace/key so

1.

  ...mapGetters([
      'cart/quantity',
      'isAuthenticated'
  ])

you'll have to access cart quantity like this['cart/quantity'] or you can rename it by using an object like

  ...mapGetters({
      cartQuantity: 'cart/quantity',
      isLoggedIn: 'isAuthenticated'
  }) 
  ...mapGetters('cart', [
      'quantity',
      'totalSum'
  ])
  ...mapGetters({
      cartQuantity: 'cart/quantity',
      prodProducts: 'prods/products'
  }) 
  • Related