首页 > 解决方案 > 如何在 vue.js 中的 v-if 中使用 toLowerCase()?

问题描述

在 vue.js 中,我有以下代码:

 <q-btn @click="goEditMode" color="primary" icon="edit" label="Edit"
     v-if="this.form.userName.toLowerCase() !== 'admin'" />

添加toLowerCase()后出现错误。

这是错误:

[Vue warn]: Error in render: "TypeError: Cannot read property 'toLowerCase' of undefined"

请帮助我,谢谢!

标签: javascriptvue.js

解决方案


发生此错误是由于undefined.toLowerCase().

看起来您正在运行toLowerCase其父级中不存在的某些属性。

所以先检查是否this.form.userName存在。

您可以执行类似的操作。

this.form.userName ? this.form.userName.toLowerCase() : ''

推荐阅读