gpt4 book ai didi

javascript - 在Vue js中,如何分配在页面重新加载时不会重置的唯一ID

转载 作者:行者123 更新时间:2023-12-02 17:59:07 25 4
gpt4 key购买 nike

我正在开发一个小元素来学习 Vue js。它应该是一个习惯追踪器。我目前有一个错误,如果您重新加载页面并添加新的习惯,我应该更改背景的功能将无法正常工作。

here it shows the bug这是我的数组的样子:

0
:
{id: 0, title: "1", ready: false}

1
:
{id: 1, title: "123", ready: false}

2
:
{id: 2, title: "123", ready: false}

3
:
{id: 0, title: "123", ready: true}

我明白为什么它不起作用,因为我使用计数器来分配 id,重新加载时重置为 0。

<div class="q-pa-md" v-for="(habit, index) in habits" :key="habit.id">
<q-card class="my-card" :id="habit.id" ref="card">
<q-card-section>
<q-checkbox
id="checkbox"
v-model="habit.ready"
@click="changeToTransparent(habit)"
>
</q-checkbox>
{{ habit.title }}

<q-btn-dropdown flat class="more" icon="more_horiz">
<q-list>
<q-item clickable v-close-popup @click="deletehabit(index)">
<q-item-section>
<q-item-label>Delete</q-item-label>
</q-item-section>
</q-item>

<q-item clickable v-close-popup @click="edithabitbutton(index)">
<q-item-section>
<q-item-label>Edit</q-item-label>
</q-item-section>
</q-item>
</q-list>
</q-btn-dropdown>
</q-card-section>
</q-card>
<div>

let counter = 0;
const habits = ref([]);


const addHabit = () => {

habits.value.push({ id: counter++, title: habittitle.value, ready: false });
savetolocalstorage();
habittitle.value = "";
};

const changeToTransparent = (habit) => {

if(document.getElementById(habit.id) != null) {
if (habit.ready) {
document.getElementById(habit.id).style.backgroundColor =
"rgba(170,193,200,0.25)";
savetolocalstorage();
} else {
document.getElementById(habit.id).style.backgroundColor = "";
savetolocalstorage();
}
}

}

关于如何解决这个问题有什么想法吗?

最佳答案

您需要加载本地存储并将长度设置为计数器值。我在这里做了一个工作示例。我还改进了你的代码,使其更符合 Vue 的概念。正如 @Rahul Purohit 指出的,您需要在保存时对结果进行 JSON.stringify 处理,并在加载时对结果进行 JSON.parse 处理。

<template>
<q-input label="Title" v-model="habitTitle" />
<q-btn label="Add habit" @click="addHabit" />
<div class="q-pa-md" v-for="(habit, index) in habits" :key="habit.id">
<q-card
class="my-card"
:id="habit.id"
:ref="(el) => (cardRefs[habit.id] = el)"
>
<q-card-section>
<q-checkbox
id="checkbox"
v-model="habit.ready"
@click="changeToTransparent(habit)"
>
</q-checkbox>
{{ habit.id }} {{ habit.title }}

<q-btn-dropdown flat class="more" icon="more_horiz">
<q-list>
<q-item clickable v-close-popup @click="deletehabit(index)">
<q-item-section>
<q-item-label>Delete</q-item-label>
</q-item-section>
</q-item>

<q-item clickable v-close-popup @click="edithabitbutton(index)">
<q-item-section>
<q-item-label>Edit</q-item-label>
</q-item-section>
</q-item>
</q-list>
</q-btn-dropdown>
</q-card-section>
</q-card>
</div>
</template>

<script setup>
const { ref, onMounted } = require("vue");

// it can be just a let.
const counter = ref(0);
const habits = ref([]);
const habitTitle = ref("test");
const cardRefs = ref({});

const saveToLocalStorage = () => console.log("saved");

const addHabit = () => {
habits.value.push({
id: counter.value++,
title: habitTitle.value,
ready: false,
});
saveToLocalStorage();
habitTitle.value = "";
};

const changeToTransparent = (habit) => {
if (cardRefs.value[habit.id] != null) {
if (habit.ready) {
cardRefs.value[habit.id].$el.style.backgroundColor =
"rgba(170,193,200,0.25)";
saveToLocalStorage();
} else {
cardRefs.value[habit.id].$el.style.backgroundColor = "";
saveToLocalStorage();
}
}
};

onMounted(() => {
// Load habits from localStorage
// This is just an example
habits.value = [
{
id: 0,
title: "Testing new habit",
ready: true,
},
{
id: 1,
title: "Testing new habit",
ready: false,
},
{
id: 2,
title: "Testing new habit",
ready: false,
},
];
counter.value = habits.value.length;
});
</script>

关于javascript - 在Vue js中,如何分配在页面重新加载时不会重置的唯一ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74873632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com