【问题标题】:How do I get my delete function to work in Vue JS?如何让我的删除功能在 Vue JS 中工作?
【发布时间】:2021-12-28 18:35:05
【问题描述】:

我正在开发一个网站,但遇到了一些问题。我对编码很陌生,所以请原谅我的新手,但我很难创建一个功能性的删除用户按钮。当我单击删除时,它会将我带到删除 url,但不会从我的主表或 mysql 数据库中删除。如果有任何建议可以幸免,将不胜感激。这是应该与问题相关的代码。

主页表;

<template>
  <div class="between:flex bottom:margin-3">
    <div class="center: flex-items">
      <span class="right:margin-1">Show</span>
      <select v-model="currentEntries" class="select" @change="paginateEntry">
        <option v-for="se in showEntries" :key="se" :value="se">
          {{ se }}
        </option>
      </select>
      <span class="left:margin-1">Entries</span>
      <div class="end:flex"></div>
    </div>
  </div>
  <div id="tableHolderDiv">
    <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col">ID</th>
          <th scope="col">Location</th>
          <th scope="col">End User</th>
          <th scope="col">Order Number</th>
          <th scope="col">Date</th>
          <th scope="col">Application</th>
          <th scope="col">Service Tech</th>
          <th scope="col">Department</th>
          <th scope="col">Hours</th>
          <th scope="col">Travel Hours</th>
          <th scope="col">Contact Name</th>
          <th scope="col">Reason</th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="row in serviceEntries"
          :key="row.id"
          @click="alertID(row.id)"
        >
          <th scope="row">{{ row.id }}</th>
          <td>{{ row.location }}</td>
          <td>{{ row.end_user }}</td>
          <td>{{ row.order_number }}</td>
          <td>{{ row.date }}</td>
          <td>{{ row.application }}</td>
          <td>{{ row.service_tech }}</td>
          <td>{{ row.department }}</td>
          <td>{{ row.hours }}</td>
          <td>{{ row.travel_hours }}</td>
          <td>{{ row.contact_name }}</td>
          <td>{{ row.reason }}</td>
          <a
            href="/delete/{{this.id}}"
            type="button"
            class="btn btn-light btn-small"
            onclick="event.stopPropagation(); return confirm('Are you sure you want to delete this entry?');"
            ><i class="bi bi-trash"></i> Delete</a
          >
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: "ServiceTable",
  computed: {
    serviceEntries() {
      return this.$store.state.serviceEntries;
       
    },
  },
  methods: {
    alertID(id) {
      this.$router.push({
        path: `/modify/${id}`,
      });
    },
  },
};
</script> 

唯一的删除调用;在我的 app.js 中

app.delete("/api/service/:id", (req, res) => {
  // console.log("DELETE /api/user/" + req.params.id + " called");
  pool.query(
    "DELETE FROM `master` WHERE id = ?",
    [req.params.id],
    function (error, results, fields) {
      if (error) res.json(error);
      else res.json(results);
    }
  );
});

我可能需要添加一个 app.post 以进行删除吗?

【问题讨论】:

    标签: javascript html css vue.js


    【解决方案1】:

    a 标签正在使用 GET-request,您需要 DELETE-request。使用button 和一个处理程序,您可以在其中使用诸如axiosgot 之类的包来发出此类请求(以及其他GET、POST、PUT)。

    <button
      class="btn btn-light btn-small"
      onclick="onDelete">
      <i class="bi bi-trash"></i>
      Delete
    </button>
    
    <script>
    import axios from 'axios';
    
    export default {
      methods: {
        async onDelete() {
          if(!confirm('Are you sure you want to delete this entry?') {
            return
          }
          await axios.delete(`/delete/${this.id}`);
        },
        alertID(id) {
          this.$router.push({
            path: `/modify/${id}`,
          });
        },
      },
    };
    </script> 
    

    【讨论】:

    • 感谢您的回复。我正在使用 axios,我尝试了上面的代码,但没有雪茄。我的代码将我带到我指定的删除网址,而您慷慨提供的代码以某种方式将我带到我的编辑页面。
    • 至少你需要在axios.delete调用中指定一个API路由,比如/api/service/${this.id}(如果你已经在axios默认设置了基本URL)而不是/delete/${this.id}。我只是在模板中使用您的 URL 指出它。
    猜你喜欢
    • 2021-12-23
    • 2021-07-04
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多