如何使用 axios vue.js 來使用 api
P粉277824378
P粉277824378 2024-02-25 18:41:54
0
1
381

我有一個單選按鈕汽車品牌列表,選擇某個品牌後如何從伺服器獲取一系列車型並將其顯示在新頁面上?為此需要採取哪些具體行動?非常感謝您閱讀這篇文章

<script>
export default {
  name: 'Brands',
  data() {
    return {
      brands: []
    }
  },
  methods: {
    next: function () {
      this.$router.push({path: '/model'})
    },
  },
  mounted() {
    this.axios
        .get('https:***')
        .then(response => (this.brands = response.data.brands))
        .catch(error => {
          console.log(error)
        })
  },
}
</script>
<template>
  <div class="alphabet-wrap">
    <ul class="alphabet">
      <li v-for="brand in brands"
          :key="brand.name"
          :class="brand.id"
      >
        <label :for="brand.id"
               @change="next"
        >
          <input type="radio"
                 :id="brand.id"
                 name="brand"
          >
          <span class="text">{{ brand.name }}</span>
        </label>
      </li>
    </ul>
  </div>
</template>

P粉277824378
P粉277824378

全部回覆(1)
P粉662361740

首先,使用v-model將選定的品牌儲存在資料中


現在您應該在查詢參數中傳遞品牌名稱,如下所示:

this.$router.push({
 path: '/models',
 query: {
   brand: this.selectedBrand
 }
})

// Route will be like https://example.com/models?brand=brand-name

在渲染/models頁面的元件中,呼叫帶有品牌名稱的api請求




更新 selectedBrand 儲存在元件資料中

//...
data() {
   return {
     selectedBrand: ""
   };
}
//...
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!