简体   繁体   English

Vue - @click 使用 Axios 在两个 API 之间发送 id 值

[英]Vue - @click to send id value between two API using Axios

I'm stuck at this problem and can't find solution on Vue.JS docs.我被困在这个问题上,无法在 Vue.JS 文档中找到解决方案。
This is what I'm getting:这就是我得到的:
I have a list with cars models from a API, that have "name" and "id", so I after I click on "Show Data" I should send id for a second API call and show all car brands for that model.我有一个 API 的汽车型号列表,有“名称”和“ID”,所以在我点击“显示数据”后,我应该发送 id 给第二个 API 电话,并显示 model 的所有汽车品牌。
I've created a alert to see if @click works fine and collect the ID that I need.我创建了一个警报来查看@click 是否正常工作并收集我需要的 ID。
On my alert, I'm getting only the model name or id, and I can't send this to my second api url.在我的警报中,我只得到 model 名称或 ID,我无法将其发送到我的第二个 api url。
This is my data where I get array items:这是我获取数组项的数据:

  const API_URL = "https://parallelum.com.br/fipe/api/v1/carros/marcas";
new Vue({
  el: "#app",
  data() {
    return {
      brands: [],
      models: [],
      loading: true,
      errored: false,
    };
  },
  mounted() {
    this.getModels();
    this.getBrands();
  }, 

methods with API calls:调用 API 的方法:

methods: {
async getBrands() {
  await axios
    .get(API_URL)
    .then((response) => {
      this.brands = response.data;
      console.log(response.data);
    })
    .catch((error) => {
      console.log(error);
      this.errored = true;
    })
    .finally(() => (this.loading = false));
},
getBrand() {
  axios
    .get(
      "https://parallelum.com.br/fipe/api/v1/carros/marcas/"+this.brandSelected+"/modelos"
    )
    .then((response) => {
      this.models = response.data.modelos;
      console.log(response.data.modelos);
    });
},  

and my @click with modelSelected:和我的@click with modelSelected:

brandSelected (brand){
  this.brands = brand.codigo;
  alert(model.codigo);
}
  },
});

I'm not getting my second api call working with this.brandSelected with id from first api. This is my HTML where I have a call for first api and @click button:我没有收到我的第二个 api 呼叫与第一个 api 的 id 的 this.brandSelected 一起工作。这是我的 HTML,我在这里呼叫第一个 api 和@click 按钮:

 <tbody v-for="(brand, index) in brands" :key="index">
              <tr>
                <td>{{brand.nome}} com código {{brand.codigo}}</td>
                <td>
                  <a
                    @click="brandSelected(brand)"
                    data-toggle="modal"
                    data-target="#modelsModal"
                    >Ver modelos</a
                  >
                </td>

I can see the model name inside my alert, using my code... But can't send to second api using this.brandSelected?我可以在我的警报中看到 model 名称,使用我的代码...但是不能使用 this.brandSelected 发送到第二个 api? Where I'm doing this wrong?我在哪里做错了? Can't find it!找不到!


Here you can check data from api: brand: 在这里可以查看来自api的数据:品牌:
Object
anos: Array [5] // not used
modelos: Array [5]
0: Object
nome: "AMAROK CD2.0 16V/S CD2.0 16V TDI 4x2 Die"
codigo: 5585
1: Object
nome: "AMAROK CD2.0 16V/S CD2.0 16V TDI 4x4 Die"
codigo: 5586
2: Object
nome: "AMAROK Comfor. CD 2.0 TDI 4x4 Dies. Aut."
codigo: 8531
3: Object
nome: "AMAROK CS2.0 16V/S2.0 16V TDI 4x2 Diesel"
codigo: 5748
4: Object
nome: "AMAROK CS2.0 16V/S2.0 16V TDI 4x4 Diesel"
codigo: 5749

models with brand code:带有品牌代码的型号:

 Object anos: Array [5] // not used modelos: Array [5] 0: Object nome: "AMAROK CD2.0 16V/S CD2.0 16V TDI 4x2 Die" codigo: 5585 1: Object nome: "AMAROK CD2.0 16V/S CD2.0 16V TDI 4x4 Die" codigo: 5586 2: Object nome: "AMAROK Comfor. CD 2.0 TDI 4x4 Dies. Aut." codigo: 8531 3: Object nome: "AMAROK CS2.0 16V/S2.0 16V TDI 4x2 Diesel" codigo: 5748 4: Object nome: "AMAROK CS2.0 16V/S2.0 16V TDI 4x4 Diesel" codigo: 5749

You seem to be treating this.brandSelected like a string in your URL but it only appears to be defined as a method.您似乎将this.brandSelected中的字符串,但它似乎仅被定义为一种方法。

It seems you want to do two things when you click a brand ...当你点击一个品牌时,你似乎想做两件事......

  1. Store the selected brand somewhere, and将所选品牌存储在某处,并且
  2. Load the brand's models加载品牌的模型

Try something like this and make sure to check your browser console for the log messages and any errors...尝试这样的事情,并确保检查您的浏览器控制台是否有日志消息和任何错误......

new Vue({
  el: "#app",
  data: () => ({
    brands: [],
    models: [],
    selectedBrand: null, // store the selected brand in this property
    loading: true,
    errored: false
  }),
  methods: {
    async getBrands () {
      this.loading = true
      this.errored = false
      try {
        const { data } = await axios.get(API_URL)
        console.log("getBrands:", data)
        this.brands = data
      } catch (err) {
        console.error(err)
        this.errored = true
      }
      this.loading = false
    },
    async getModels (brandCode) {
      console.log("Loading brand models for brand code:", brandCode)
      this.models = []
      const { data } = await axios.get(`${API_URL}/${encodeURIComponent(brandCode)}/modelos`)
      this.models = data.modelos
    },
    brandSelected (brand) {
      console.log("brand selected:", JSON.stringify(brand))
      // store the selected brand (in case you need it)
      this.selectedBrand = brand

      // load the brand's models
      this.getModels(brand.codigo)
    }
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM