Problem: For the object obtained from vuexstore, if you print the object directly, there will be content, but if you print the property in the object, it will be undefined

Problem: For the object obtained from vuexstore, if you print the object directly, there will be content, but if you print the property in the object, it will be undefined

reason:

  • The action is an asynchronous operation, and it must be valuable to print it in the commit method,
    so why the object can be printed: because the object variable is an address, the object has been expanded when it is expanded. So you can see the value in the object.
    The object variable stores the address of the object in the memory, and the ordinary data variable is the actual value of the variable.
  • So in fact, the obtained object is actually an empty object at the moment of printing, but when it is expanded, the commit operation has been completed, so the object gets the value inside, so we can see the object in the object when printing the object properties.
  • But the ordinary type is what it is at the moment it is printed, so when we print a certain attribute in the object obtained from the store, such as allQuestionList.value, it is just an ordinary print, which is the commit operation Not finished yet, so no value.

Solution: add a timer

const allList = computed(
      () => store.state.ListModule.allListInfo
    )
console.log(allList.value)  //没有值

setTimeout(() => {
    
    
     console.log(allList.value)  //有值
    }, 1000

However, after the timer is added, the content written in the timer (variables, functions, etc.) cannot be obtained and used in the outer scope, so here the required data is cached locally through localStorage in the timer, Then in the outer layer function, by obtaining the data stored in the local cache, the data can be obtained in this way.

Guess you like

Origin blog.csdn.net/weixin_53737663/article/details/127187743