【问题标题】:Return empty string into ES6 template string将空字符串返回到 ES6 模板字符串
【发布时间】:2017-02-05 18:08:28
【问题描述】:

我正在做以下事情:

return this.innerHTML = `
  <input 
        value = ${this.value ? this.value : ''}
        placeholder=${this.placeholder}
  ></input>
`

现在的问题是,如果 this.value 未定义,未定义的字符串设置为输入值,这就是为什么我尝试将空字符串返回到输入值属性。这不起作用,然后我将placeholder=${this.placeholder} 作为字符串放入 value 属性中。

我在谷歌上安静了一会儿,找不到任何答案,这可能吗?

我们将不胜感激。

【问题讨论】:

  • 您可以使用this.value || '' 代替this.value ? this.value : '' 以简化和可读性。

标签: javascript ecmascript-6 template-strings


【解决方案1】:

您没有用双引号将值和占位符括起来。

return this.innerHTML = `
  <input 
        value = "${this.value ? this.value : ''}"
        placeholder="${this.placeholder}"
  ></input>
`

【讨论】:

    猜你喜欢
    • 2016-09-19
    • 2015-02-18
    • 2011-02-02
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多