大佬教程收集整理的这篇文章主要介绍了在三元运算符中使用多个条件时,我的 Vue 应用程序无法加载,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我曾经在一个天气显示应用中学习 Vue 这是应用程序运行良好的原始表达式
@H_772_5@<div ID="app" :class="typeof weather.main != 'undefined' && weather.main.temp < 16 ? '' : 'warm' ">
但是当我将表达式更改为 -
@H_772_5@<div ID="app" :class="typeof weather.main != 'undefined' && weather.main.temp < 16 ? '' : weather.main.temp < 18 ?'chill': weather.main.temp < 30 ?'warm':'hot' ">
应用程序编译但它不加载任何东西并且控制台是空白的没有错误。
问题是operator precendece。表达式计算为
@H_772_5@(typeof weather.main != 'undefined' && weather.main.temp < 16) ? '' : (weather.main.temp < 18 ?'chill': weather.main.temp < 30 ?'warm':'hot')
当 @H_772_5@weather.main.temp < 18 未定义时,这会导致评估 @H_772_5@weather.main。
解决方案是不要将不可读的代码放入模板中,并将其移动到计算属性中,在那里它可以以易于理解和维护的形式编写。 @H_772_5@typeof 检查未定义值只需要潜在未定义的变量。可选链可用于 Vue 2 中的计算属性以避免条件检查,以及 Vue 3 中的模板。
可以是:
@H_772_5@computed: { appClass() { if (!weather.main || weather.main.temp < 16) return ''; else if (weather.main.temp < 18) return 'chill'; else if (weather.main.temp < 30) return 'warm'; else return 'hot'; } }
以上是大佬教程为你收集整理的在三元运算符中使用多个条件时,我的 Vue 应用程序无法加载全部内容,希望文章能够帮你解决在三元运算符中使用多个条件时,我的 Vue 应用程序无法加载所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。