简体   繁体   English

如何在 JavaScript 的构造函数中使用模板文字

[英]How to use template literals in a constructor in JavaScript

I'm building a To Do list app and have a question regarding OOP and JavaScript.我正在构建一个待办事项列表应用程序,并有一个关于 OOP 和 JavaScript 的问题。 I want to create a value in the Constructor that holds my taskBody which contains the HTML and template literal that will be assigned by either the input value or an eventual population from local storage .我想在包含我的taskBody的构造函数中创建一个值,其中包含将由输入值或本地存储中的最终填充分配的 HTML 和模板文字。 My goal is to re-use this HTML in two separate functions, but I'm stuck with the template literal.我的目标是在两个单独的函数中重用这个 HTML,但我坚持使用模板文字。

class Task {
  constructor() {
    let taskValue //Initializing a variable

    this.taskBody = `<div class="task">
    <span>${taskValue}</span> //Template Literal
      <span class="actions">
       
        <a class="edit-button" title="Edit Task">Edit</a>
        <button class="complete-button" title="Complete Task"><i class="fas fa-check"></i></button>
        
      </span>
    </div>`;

  }

  addTask = () => {
    //Prevent empty task
    if (this.input.value == "") {
      this.setPlaceholder("Please Enter A Task");
      return;
    }

    this.taskValue = this.input.value; //Trying to reassign taskValue to the input value

    this.results.innerHTML += this.taskBody; //Trying to grab the HTML from the constructor and populate with taskValue able

    ls.setLS(this.taskValue); //setting the Local Storage the Task Value, which works
  };

}

I expect if I type "Stack Overflow" in the to-do list, "Stack Overflow" populates in the HTML and the Local Storage, however, it only populates in the Local Storage.我希望如果我在待办事项列表中输入“堆栈溢出”,“堆栈溢出”会填充在 HTML 和本地存储中,但是,它只填充在本地存储中。 The todo item is either undefined, null, or empty.待办事项要么是未定义的,要么是空的,要么是空的。

I've tried using this.taskValue , let taskValue = "" , and let taskValue = null , but I get the results described above.我试过使用this.taskValuelet taskValue = ""let taskValue = null ,但我得到了上述结果。 Where am I going wrong, and more specifically, how can I reuse the HTML in different functions?我哪里出错了,更具体地说,我如何在不同的功能中重用 HTML?

Here's a CodePen where you can see the issue:这是一个 CodePen,您可以在其中看到问题:

Codepen代码笔

When you first instantiate the Task , the value of the this.taskBody is set as below:当您第一次实例化Taskthis.taskBody的值设置如下:

<div class="task">
    <span>undefined</span>
    <span class="actions">
       
        <a class="edit-button" title="Edit Task">Edit</a>
        <button class="complete-button" title="Complete Task"><i class="fas fa-check"></i></button>
        
    </span>
</div>

with undefined value, because at the moment of instantiation, the taskValue is undefined.具有undefined值,因为在实例化的那一刻, taskValue是未定义的。

If your todo list items are added dynamically (which is the case), consider having a function which will enable dynamic replacement, like:如果您的待办事项列表项是动态添加的(就是这种情况),请考虑使用一个启用动态替换的功能,例如:

getTaskBody = item => `<div class="task">
    <span>${item}</span>
    <span class="actions">
       
        <a class="edit-button" title="Edit Task">Edit</a>
        <button class="complete-button" title="Complete Task"><i class="fas fa-check"></i></button>
        
    </span>
</div>`;

and use it later in line 123, instead of:并稍后在第 123 行使用它,而不是:

this.results.innerHTML += this.taskBody;

do:做:

this.results.innerHTML += getTaskBody(this.taskValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM