How does vue ensure that it gets the data before rendering, which is equivalent to blocking the life cycle?

request data asynchronously, but lifecycle functions are also asynchronous, so how can you ensure that you get the data before rendering?

Mar.05,2021

before rendering, created,beforeCreate starts to adjust the interface, and then the page is empty as soon as it comes in, and the corresponding part is displayed after it is returned. In this way, rendering and request are asynchronous, waiting for the request to come back before releasing the page (v-if).


your request can be understood in this way?
after I request an API, in beforeCreate, I don't want VUE to continue until the data comes back before executing the life cycle of VUE?
if this is the case, it cannot be done.

for a simple workaround, you can use the v-if idea upstairs.

in addition, it can also be done by the authorities, and it can be done this way.
requests data before the route jumps to this point, and after the next () operation is performed, the lifecycle of VUE execution begins.
data: get data before navigation


ajax is an asynchronous operation. Vue cannot guarantee to get the data before rendering. What vue can do is initialize the visibility:hidden of the page and display the data again.


positive solution upstairs. Due to the existence of asynchronous operation, vue cannot guarantee to get the data before the page is rendered, so the general practice is to hide the page, call the API to obtain the data when created or beforeCreate, and control the page display in the callback function after successfully getting the data. This can avoid the problem of page jumping to the greatest extent


the solution to this problem should be to add a loading. Adding a loading when obtaining data asynchronously means that you are now getting the data, and then rendering it again after obtaining the data


blocking. You can add loadding to avoid page whitespace, and the other means that the page will not jump when the data is loaded again


change the request data into Synchronize


this hook that should be routed

beforeRouterEnter(to,from,next) {
    doAjax.then(res => {
        // 
        next(vm => {
            // 
        })
    })
}
Menu