【问题标题】:How to store individual CSS styling in a database?如何将单个 CSS 样式存储在数据库中?
【发布时间】:2020-12-06 12:12:36
【问题描述】:

感谢您帮助人们并为开源做出贡献。

我有一个 Vue.js 待办事项应用程序。数据通过 vue-fire 和 firestore 存储在 firebase 中。 我有一个功能可以通过单击来检查待办事项。但是每当刷新页面时,这种样式就会消失。

我怎样才能使“line-through”样式(或任何其他)存储在数据中,以便即使在刷新时它也保留在那里?

   <template>
      <div class="pt-3">
        <div v-show="showText == true" class="text">
          <h2>Get started by adding a new todo.</h2>
          <p>Click on the item to check it done.</p>
          <p class="warnings">This is a prototype app. Please do not input any sensitive information!</p>
        </div>
        <v-row class="addtodo d-flex justify-center">
          <v-col cols="8">
            <v-text-field
              class="addtext"
              label="New Todo"
              outlined
              v-model="newItem"
              @keyup.enter="addItem"
            ></v-text-field>
          </v-col>
          <v-col cols="1">
            <v-btn class="btn" large color="primary" :disabled="newItem == ''" @click="addItem">Add</v-btn>
          </v-col>
        </v-row>
        <transition-group class="d-flex flex-column justify-center align-center" name="fade">
          <v-card
            class="d-flex justify-center align-center ma-2 text-left"
            width="400"
            v-for="(ToDo, index) in ToDos"
            :key="ToDo.id"
        @click="checked = !checked"
          >
            <v-row class="d-flex justify-space-around">
              <v-col cols="8">
                <v-list-item-title     
            :class="{ done: checked }" class="headline mb-1">{{ index + 1}} - {{ ToDo.name }}</v-list-item-title>
              </v-col>
              <v-col class="tools text-right">
                <v-btn class="dicon" icon color="secondary" @click="editToDo(ToDo.id)">
                  <v-icon>mdi-pencil</v-icon>
                </v-btn>
                <v-btn class="dicon" icon color="red" @click="deleteToDo(ToDo.id)">
                  <v-icon>mdi-delete</v-icon>
                </v-btn>
              </v-col>
            </v-row>
          </v-card>
        </transition-group>
      </div>
    </template>
    
    
    
    <script>
    import { db } from "../firebase/db";
    
        export default {
      name: "ToDoList",
      data() {
        return {
          ToDos: [],
          ToDo: [],
          newItem: "",
          index: "",
          showText: true,
          checked: false,
          currentlyEditing: null,
          todoEditText: "",
        };
      },
      methods: {
        async addItem() {
          if (this.newItem) {
            await db.collection("ToDos").add({ name: this.newItem });
            this.newItem = "";
          }
          this.deleteText();
        },
        deleteToDo(id) {
          db.collection("ToDos").doc(id).delete();
        },
        deleteText: function () {
          this.showText = false;
        },
      },
      beforeDestroy() {
        clearInterval(this.showText);
      },
      firestore: {
        ToDos: db.collection("ToDos"),
      },
    };
    </script>


<style>
.done {
 text-decoration: line-through;
}

.text {
  margin: 0 auto;
  margin-top: -200px;
  text-align: center;
  padding: 1vh;
}
.warnings {
  color: red;
}

h2 {
  margin: 0 auto;
  text-align: center;
  margin-top: 30vh;
}
.addtodo {
  position: fixed;
  bottom: -20px;
  right: 0;
  left: 0;
  padding-right: 30px;
  z-index: 1111;
  background: white;
  box-shadow: 4px 4px 4px 4px #888;
}

.btn {
  margin-top: 6px;
}
.addtext {
}

.todolist {
  margin: 0 auto;
  color: red;
  display: flex;
  text-align: left;
}

.tools {
  position: absolute;
  top: 2px;
  padding: 0;
  margin: 0 auto;
}
</style>

【问题讨论】:

  • 如果样式在页面重新加载时消失,那么可能是您的代码没有正确发送更新到 Firebase 的问题?根据提供的代码,您已经添加了动态类绑定 :class="{ done: checked }" 所以它应该赶上虽然我看到你在那里使用 :classclass 属性(也许尝试先切换它们的顺序所以 class 然后:class 因为它可能会覆盖页面渲染上的某些内容?(只是假设,不确定)
  • 是的,但这就是问题所在。我不能让它发送那个。我不知道怎么做。我已经搜索了 2 个小时。
  • 所以您的问题不应该是关于在数据库中存储样式,而是正确连接到它 - 您发送什么请求?你收到什么回应? (可能是错误代码?)
  • 这可能是错误的,但我解释了我想做什么。就是这个标题,不知道怎么写更好。
  • 我想我理解错了-抱歉-已经添加了答案-希望对您有所帮助;)

标签: javascript css firebase vue.js vuefire


【解决方案1】:

我没有看到任何 update 方法可以让您更新数据库中的记录 - 这里有一些可以用来应用的 sn-p:

首先,绑定正确的点击事件(而不是现有的@click="checked = !checked"一个):

@click="updateTodo(ToDo)"

然后在你的方法中:

updateToDo(ToDo) {
  db.collection("ToDos").doc(ToDo.id).patch({ checked: !ToDo.checked })  // <-- not sure if you have .patch method available here to update only 1 property, but I hope you can figure out how to handle that :)
}

编辑(下面第一条评论的可能答案):

不确定您与 DB 的绑定是如何工作的,但我认为它可能会这样工作:

updateToDo(ToDo) {
  ToDo.checked = !ToDo.checked;
  db.collection("ToDos").doc(ToDo.id).patch({ checked: ToDo.checked });
}

【讨论】:

  • 这将是一个不同的问题,但我正在使用 vuetify 并且我的方法 checked = !checked 不再起作用,或者它检查了所有待办事项,所以我使用这个:click="$event.target. classList.toggle('checked')" 有没有办法将它添加到上面的代码中以从数据库更新它?谢谢你。对不起这个问题
  • @sunflowerseed 我已经更新了我的答案,但我不确定这是否会如你所愿 - 请让我知道它是否有效(如果没有,请告诉我什么 DB 插件/controller 你用过,所以我可以帮忙)
猜你喜欢
  • 2014-12-05
  • 2012-09-26
  • 2013-08-14
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
相关资源
最近更新 更多