最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bind vuex state to this in data()? - Stack Overflow

programmeradmin1浏览0评论

I'm upgrading vuex to 2.0 and was wondering if its possible use mapstate/getters before data gets initialized?

In Vuex 1.0, the vuex state would be mapped before data() did so I could just call this and then the state I wanted to access

import { mapGetters } from 'vuex'

export default {
  vuex: {
    getters: {
      userSettings: ({ settings }) => settings.userSettings,
    }
  },
  data: function () {
    return {
      sendEmails: this.userSettings.sendEmails
    }
  }
}

But in Vuex 2.0, I have to do this.$store.state.settings.UserSettings.sendEmails

import { mapGetters, mapState } from 'vuex'

export default {
data: function () {
   return {
    sendEmails: this.$store.state.settings.UserSettings.sendEmails
   }
}
puted: {
  ...mapGetters({
    settings: "settings"
  })
}

Is there a way to have that state initialized before data()? I have multiple ponents that make use of the state in the data initialization and having to call this.$store.state? I realize I can do some destructuring but I was just wondering if I could avoid that.

I'm upgrading vuex to 2.0 and was wondering if its possible use mapstate/getters before data gets initialized?

In Vuex 1.0, the vuex state would be mapped before data() did so I could just call this and then the state I wanted to access

import { mapGetters } from 'vuex'

export default {
  vuex: {
    getters: {
      userSettings: ({ settings }) => settings.userSettings,
    }
  },
  data: function () {
    return {
      sendEmails: this.userSettings.sendEmails
    }
  }
}

But in Vuex 2.0, I have to do this.$store.state.settings.UserSettings.sendEmails

import { mapGetters, mapState } from 'vuex'

export default {
data: function () {
   return {
    sendEmails: this.$store.state.settings.UserSettings.sendEmails
   }
}
puted: {
  ...mapGetters({
    settings: "settings"
  })
}

Is there a way to have that state initialized before data()? I have multiple ponents that make use of the state in the data initialization and having to call this.$store.state? I realize I can do some destructuring but I was just wondering if I could avoid that.

Share Improve this question asked Apr 12, 2017 at 16:55 cvdvcvdv 2,9423 gold badges18 silver badges32 bronze badges 3
  • sendEmails should be a puted property, you can eventually create a getter for it (in your store). – soju Commented Apr 12, 2017 at 19:42
  • @soju can you explain why it must be a puted property? – Eric Guan Commented Apr 13, 2017 at 21:33
  • 1 Did you read this : vuex.vuejs/en/… ? – soju Commented Apr 14, 2017 at 7:31
Add a ment  | 

1 Answer 1

Reset to default 7

I would set sendEmails in mounted

import { mapGetters, mapState } from 'vuex'

export default {
    data: function () {
       return {
        sendEmails: []
       }
    }

    puted: {
      ...mapGetters({
        settings: "settings"
      })
    },

    mounted: function() {
       if (this.settings.UserSettings){
          this.sendEmails = this.settings.UserSettings.sendEmails
       }
    }
}
发布评论

评论列表(0)

  1. 暂无评论