Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change of state not dispatch to component #1658

Closed
galenyuan opened this issue Dec 25, 2019 · 2 comments
Closed

Change of state not dispatch to component #1658

galenyuan opened this issue Dec 25, 2019 · 2 comments

Comments

@galenyuan
Copy link

@galenyuan galenyuan commented Dec 25, 2019

Version

3.1.2

Reproduction link

http://jsfiddle.net/sx9e7fav/

Steps to reproduce

Open the reproduce link.

What is expected?

should display email list

What is actually happening?

The placeholder value 「-」 displayed


jsfiddle can not use Vue devtool, so you can copy this code in a html file and run a HTTP Server.

<html>
  <head>
    <title>demo</title>
    <script src="https://unpkg.com/vue@2.6.11/dist/vue.min.js"></script>
    <script src="https://unpkg.com/vuex@3.1.2/dist/vuex.min.js"></script>
  </head>
  <body>
    <ul id="demo">
      <li v-for="item in list">{{ users[item.userid] ? users[item.userid].email : '-' }}</li>
    </ul>
    <script>
      Vue.config.devtools = true
      const getUserById = (id) => {
        return new Promise((resolve) => {
          setTimeout(() => {
            resolve({
              id,
              email: `${id}@email.com`
            })
          }, 1000)
        })
      }
      const store = new Vuex.Store({
        state: {
          users: {}
        },
        mutations: {
          addUser(state, user) {
            state.users[user.id] = user
          }
        },
        actions: {
          async getUser(ctx, id) {
            if (id in ctx.state.users) return
            ctx.commit('addUser', { id, email: '-' })
            const user = await getUserById(id)
            ctx.commit('addUser', user)
          }
        }
      })
      const app = new Vue({
        store,
        el: '#demo',
        data: {
          list: [
            { userid: 1 },
            { userid: 2 },
            { userid: 3 },
            { userid: 4 },
            { userid: 5 },
          ]
        },
        computed: {
          ...Vuex.mapState(['users'])
        },
        methods: {
          ...Vuex.mapActions(['getUser'])
        },
        created() {
          this.list.forEach(item => {
            this.getUser(item.userid)
          })
        }
      });
    </script>
  </body>
</html>

Let's check in Vue devtool, we can find mutaion has triggered and state changed:
image

Then lets try a Time Travel:
image

Then the page gose to what I want:
image

@galenyuan

This comment has been minimized.

Copy link
Author

@galenyuan galenyuan commented Dec 25, 2019

not sure my code is clear enough, if anything confused, let me know pls

@ktsn

This comment has been minimized.

Copy link
Member

@ktsn ktsn commented Dec 25, 2019

@ktsn ktsn closed this Dec 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.