将两个变量存储到localStorage时如何连接?

How to connect two variables when storing it to localStorage?

我正在 Ionic 上编写我的第一个应用程序。这是一个简单的笔记应用程序。我正在这样保存我的笔记列表:

save() {
    if(this.todoItem != "" && this.todoText != "") {
        this.todoList.push(this.todoItem);
        localStorage.setItem("todol", JSON.stringify(this.todoList));
        this.nav.pop();
    }
}

我想连接 todoItem 和 todoText,所以当我将它推送到本地存储时 JSON 看起来像这样:

{"todoList":[
    {"todoItem":"item1", "todoText":"text1"},
    {"todoItem":"item2", "todoText":"text2"},
    {"todoItem":"item3", "todoText":"text3"}
]} 

除了将普通的 todoItem 推送到列表之外,您还可以尝试使用您需要的模式构建一个对象,如图所示。

    save() {
    if(this.todoItem != "" && this.todoText != "") {

        this.todoList.push({"todoItem":this.todoItem , "todoText":this.todoText});
        localStorage.setItem("todol", JSON.stringify(this.todoList));
        this.nav.pop();
    }
}