Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Vue mapState state

In my computed, I have two "loading" states that I want to track. Is there a way to add an alias to the other state with this syntax?

  computed: {
    ...mapState('barcodes', ['barcodes', 'loading', 'pagination']),
    ...mapState('users', ['user', 'loading']), // add an alias for this "loading"
  }
like image 878
patrick_star Avatar asked Oct 23 '25 07:10

patrick_star


2 Answers

mapState has supported an object way, something like: (Specific alias for only 1 field)

computed: {
  ...mapState('barcodes', {
    barcodesLoading: state => state.loading,
  })
}

Or by namespace: (If you want to wrap all user state into the userData namespace)

computed: {
  ...mapState({
      barcodeData: 'barcodes',
      userData: 'users'
  })
}

on Template:

<div>{{barcodeData.loading}}</div>

There is another thread for your reference:

vuex namespaced mapState with multiple modules

like image 93
huan feng Avatar answered Oct 24 '25 22:10

huan feng


I'm not absolutely sure, because I can't check it out right now. But AFAIK you should just try this:

computed: {
    ...mapState('barcodes', {barcodes:'barcodes', alias1:'loading', pagination:'pagination'}),
    ...mapState('users', {user:'user', alias2:'loading'}), // add an alias for this "loading"
  }

I.e. use object instead of array.

like image 30
Mikhail Semikolenov Avatar answered Oct 24 '25 20:10

Mikhail Semikolenov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!