1. 当我们没有定义数据类型时,后续可能会提前获取一些不存在的数据,用于v-if的渲染
例如:
```
// 用户数据
const userInfo = ref() //未定义数据类型,默认为undefined类型
//此时,若有代码需要调用 userInfo.token 用于验证是否渲染某些标签时,报错就来了!!!
<template v-if="userStore.userInfo.token"> </template>
//其中userStore.userInfo.token 只有当登录后调用接口才可以获得
```
可能出现的错误:
···
[Vue warn]: Unhandled error during execution of setup function
at <LayoutNav >
at <Index onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > >
at <RouterView >
at <App>
main.js:12 [Vue warn]: Unhandled error during execution of component update
at <RouterView >
at <App>
pinia.js?v=1ff9b1a1:189 🍍 "category" store installed 🆕
main.js:12 [Vue Router warn]: uncaught error during route navigation:
main.js:12 TypeError: Cannot read properties of undefined (reading 'token')
at defineStore.persist (cart.js:11:40)
at defineStore.persist (user.js:13:23)
cart.js:11 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'token')
at defineStore.persist (cart.js:11:40)
at defineStore.persist (user.js:13:23)
request.js:42 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
at request.js:42:38
at async Proxy.getCategory (category.js:12:17)
at async Proxy.getCategory (category.js:12:17)
···
总结:创建ref时,最好定义好数据类型, 血淋淋的教训!!!(如有错误,欢迎评论区指正,因为我花了一个下午,才找出来的bug!还是太菜了~)