我不想推送 null 所以我要检查一个条件,但是我认为它说有一个语法错误
“无法测试‘void’类型的表达式的真实性”
我该如何正确地做到这一点?
localStorage.setItem('todoitems', JSON.stringify(this.todoitems))
? localStorage.setItem('todoitems', JSON.stringify(this.todoitems))
: [];
Run Code Online (Sandbox Code Playgroud)
由于setItem不返回任何内容,因此它抱怨void在需要布尔值的三元运算符中使用类型
您可以if在this.todoitems之前添加条件setItem
if (this.todoitems !== null)
localStorage.setItem('todoitems', JSON.stringify(this.todoitems))
Run Code Online (Sandbox Code Playgroud)