The front end can run normally, but the console reports an error data of undefined onMounted, it is best not to use asynchronous writing

record problem

When working on the project, the project can run normally, but the console always reports an error, and one of the data is undefined.

analyse problem

Generally, when encountering this situation, Vue cannot find the data. But why does it work? The explanation is that the data cannot be found when the loading starts, but after the loading is complete, it can run normally. Although it has no effect on the project itself, it will report an error.

If this is the case, there is a direct solution, which is to assign an initial value to the variable that needs to be assigned in the onMounted life cycle.

What to do if the initial value is still wrong

This problem was encountered by me. After looking for a lot of problems, I found that when my previous colleagues were writing code, they used asynchronous writing in the onMounted life cycle function.

The emergence of asynchronous writing will cause many problems. One is the order of project loading; the other is that since it is written in async and await, using promise chain calls, the result of one request will inevitably lead to the result of the entire chain call.

Therefore, the solution proposed here is not to touch the contents of async, but to write a string of synchronous codes on it to see if the problem can be successfully resolved. If not, try changing both to sync.

== It is recommended to change them to synchronous or asynchronous with a clear execution order, otherwise it will easily lead to logical confusion and cause errors in other places. ==Another thing to remind is that when we change a place, we need to test more about whether there is a problem with the related components, otherwise an error report may bring a series of reactions.

Supongo que te gusta

Origin blog.csdn.net/zxdznyy/article/details/132353641
Recomendado
Clasificación