日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 Vue 正文

vue中判斷對(duì)象為空|cannot read property ‘xx‘ of undefined

作者:騰夢(mèng) 更新時(shí)間: 2024-03-02 Vue

vue中判斷對(duì)象為空

結(jié)構(gòu)體如下

在調(diào)用text.value時(shí),因默認(rèn)情況下text為空?qǐng)?bào)錯(cuò)

解決方法

使用 typeof 對(duì)text進(jìn)行判斷是否為 undefined

image-20210714095428450

存在問題

當(dāng)判斷text.value時(shí)會(huì)失效,因?yàn)閠ext就不存在,value更不用說了所以直接判斷text就可以了

依據(jù)

在ESLint 0.5.0中引入 typeof操作符,用于強(qiáng)制與有效的字符串進(jìn)行比較 ESLint文檔地址

typeof通常與以下字符串比較:undefinedobjectbooleannumberstringsymbolbigint

與其他字符串比較時(shí),通常是個(gè)書寫錯(cuò)誤

Options

該規(guī)則有一個(gè)對(duì)象選項(xiàng):

  • "requireStringLiterals": true 要求 typeof 表達(dá)式只與字符串字面量或其它 typeof 表達(dá)式 進(jìn)行比較,禁止與其它值進(jìn)行比較。

錯(cuò)誤 代碼示例:

/*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

選項(xiàng) { "requireStringLiterals": true }錯(cuò)誤 代碼示例:

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

選項(xiàng) { "requireStringLiterals": true }正確 代碼示例:

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

v-for與v-if 同時(shí)存在時(shí),渲染錯(cuò)誤

解決方法

外層加一個(gè)template把v-for放在template標(biāo)簽

v-for中的key理解

v-for 是循環(huán),可以把數(shù)組中的元素遍歷出來,

vue3中,必須要有key參數(shù),key就相當(dāng)于索引,

原文鏈接:https://blog.csdn.net/weixin_43815091/article/details/124383047

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新