vue中判断对象为空|cannot read property ‘xx‘ of undefined

vue中判断对象为空

结构体如下

在调用text.value时,因默认情况下text为空报错

解决方法

使用 typeof 对text进行判断是否为 undefined

image-20210714095428450

存在问题

当判断text.value时会失效,因为text就不存在,value更不用说了所以直接判断text就可以了

依据

在ESLint 0.5.0中引入 typeof操作符,用于强制与有效的字符串进行比较 ESLint文档地址

typeof通常与以下字符串比较:undefinedobjectbooleannumberstringsymbolbigint

与其他字符串比较时,通常是个书写错误

Options

该规则有一个对象选项:

  • "requireStringLiterals": true 要求 typeof 表达式只与字符串字面量或其它 typeof 表达式 进行比较,禁止与其它值进行比较。

错误 代码示例:

/*eslint valid-typeof: "error"*/

typeof foo === "strnig"
typeof foo == "undefimed"
typeof bar != "nunber"
typeof bar !== "fucntion"

正确 代码示例:

/*eslint valid-typeof: "error"*/

typeof foo === "string"
typeof bar == "undefined"
typeof foo === baz
typeof bar === typeof qux

选项 { "requireStringLiterals": true }错误 代码示例:

typeof foo === undefined
typeof bar == Object
typeof baz === "strnig"
typeof qux === "some invalid type"
typeof baz === anotherVariable
typeof foo == 5

选项 { "requireStringLiterals": true }正确 代码示例:

typeof foo === "undefined"
typeof bar == "object"
typeof baz === "string"
typeof bar === typeof qux

v-for与v-if 同时存在时,渲染错误

解决方法

外层加一个template把v-for放在template标签

v-for中的key理解

v-for 是循环,可以把数组中的元素遍历出来,

vue3中,必须要有key参数,key就相当于索引,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值