iT邦幫忙

2021 iThome 鐵人賽

DAY 17
0
Modern Web

Vue 3 請你幫我做一個網站好嗎 (っಠ‿ಠ)っ系列 第 17

Day17 - 上架後臺的商品列表頁,今天只鋪了 template

<template>
  <table class="table">
    <thead>
      <tr>
        <th scope="col">排序</th>
        <th scope="col">類別</th>
        <th scope="col">商品名稱</th>
        <th scope="col">原價</th>
        <th scope="col">售價</th>
        <th scope="col">公開上架</th>
        <th scope="col">編輯</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(product, i) in productsList" :key="product.id">
        <th scope="row">{{ i }}</th>
        <td>{{ product.category }}</td>
        <td>{{ product.origin_price }}</td>
        <td>{{ product.title }}</td>
        <td>{{ product.price }}</td>
        <td>{{ product.enabled }}</td>
        <td>
          <button type="button" class="btn btn-primary me-2">編輯</button>
          <button type="button" class="btn btn-outline-danger">刪除</button>
        </td>
      </tr>
    </tbody>
  </table>
</template>
<script>
import axios from "axios";
export default {
  name: "productsManageTable",
  data() {
    return {
      token: "",
      uuid: "---",
      productsList: [],
    };
  },
  methods: {
    // 從資料庫取得產品列表
    getProductList() {
      const api = `---`;
      // 先從瀏覽器 cookie 取得 token
      this.token = document.cookie.replace(
        /(?:(?:^|.*;\s*)loginToken\s*=\s*([^;]*).*$)|^.*$/,
        "$1"
      );

      // https://github.com/axios/axios#global-axios-defaults
      // 並且 header 按照後端 api 文件的規格要求填上 Bearer token 字樣
      axios.defaults.headers.common.Authorization = `Bearer ${this.token}`;
      axios
        .get(api)
        .then((res) => {
          console.log(res);
          this.productsList = res.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  
  // 當元件被建立好,就先取得產品列表
  created() {
    this.getProductList();
  },
};
</script>

上述內容有出入的話,希望大大們能在提點提點 (っಠ‿ಠ)っ


上一篇
Day16 - 複習 Vue 生命週期
下一篇
Day18 - 學了一點點 vuex
系列文
Vue 3 請你幫我做一個網站好嗎 (っಠ‿ಠ)っ19
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言