The front-end console reports an error #Uncaught (in promise) TypeError: Cannot read properties of undefined (), use nextTick to solve it. vue3

During page rendering, the console reported an error Uncaught (in promise) TypeError: Cannot read properties of undefined (xxxx), but the page was still rendered successfully.
The reason is that the interface request is asynchronous, and when the page is initially rendered, the value of the interface has not been obtained, so an error is reported but it is rendered normally. The solution is to judge by adding nexttick and if.
That is to say, the interface request method is written in nextTick. The following is the simulation code:

const list = ref([])
nextTick(() => {
    
    
    test().then((res) => {
    
    
      const testlist= res;
      if (typeof testlist!= "undefined") {
    
    
        list.value = testlist.list;
      }
      console.log(1)
    });
});

You can see that 1 is printed twice on the page, because the value was not obtained the first time. This operation can prevent the console from reporting an error.

Guess you like

Origin blog.csdn.net/weixin_45807026/article/details/125739372