How do i use the id coming from the API response in other parts of HTML & Scripts in VueJS

自古美人都是妖i 提交于 2021-01-29 05:07:39

问题


I am trying to make my code Dynamic without using any hard code. And for that i want to use the id coming from the API.

Here is my API Response: [/infos]

    [
        ...

      {
        "id": 7,
        "name": "Highway",
        "price": "High",
        "min": "785",
        "max": "1856",
        "tag": null,
      },
      {
        "id": 8,
        "name": "Lowway",
        "price": "low",
        "min": "685",
        "max": "1956",
        "tag": null,
      } 

      ...

    ]

Here is my Component.vue:

<div class="vx-row">
    <div class="vx-col w-full md:w-1/2 mb-base" v-for="info in infos" :key="info.id">
          <vx-card>
             <div class="header">
               <div class="left">
                 <img src="./img/info_8_logo.png" alt=""/>            <-- Hard coded value ie 8
                 <b>{{info.name}}</b>
               </div>
             <div class="right">
                  <vs-button @click="$router.push({name: 'letsCheck', params: {viewContentId: '8' }}).catch(err => {})">Explore</vs-button> 
                          <-- Above as a Hard-coded value ie 8 -->
                   {{mes.this_is_showing_only_data_of_id8}}       
             </div>
           </div>
          </vx-card>
      </div>
    </div>

<script>
infos: [],
mes: []
...
created () {
  this.$http.get('/infos')                                           
    .then((response) => { this.infos = response.data })
    .catch((error) => { console.log(error) })
   
  this.$http.get('/messages/8/tick')                                  <-- Hard coded value
    .then((response) => { this.mes = response.data })
    .catch((error) => { console.log(error) })

}
</script>

As you can see my Component.vue, i am displaying 8 cards and every cards are displaying the same contents as i have hardcoded the id as 8. So i want to know how to use the id coming from the API response of /infos endpoint in HTML and also in other scripts.

Also to note that the id coming from the API is just a number, so i want to know how to convert it to a string. And then how it can be used in other HTML and script.

Please help me in this i want to put the id coming from API to whichever comments i have made in Component.vue.


回答1:


Best Practises here, you need to create a new component for your messages and in the component pass as props the id and having this id to build dynamically the message component. Message.vue

    <template>
             <div class="right">
                  <vs-button @click="$router.push({name: 'letsCheck', params: {viewContentId: id }}).catch(err => {})">Explore</vs-button> 
                         
                   {{mes.my_key_name}}       
             </div>
           </template>

<script>
mes: []
...
  props: {
    id: {
      type: String,
      required: true,
    },
  },
created () {  
  this.$http.get(`/messages/this.id/tick`)                                 
    .then((response) => { this.mes = response.data })
    .catch((error) => { console.log(error) })

}
</script>

App.vue will be:

<div class="vx-row">
    <div class="vx-col w-full md:w-1/2 mb-base" v-for="info in infos" :key="info.id">
          <vx-card>
             <div class="header">
               <div class="left">
                 <img :src="`./img/info_${info.id}_logo.png`" alt=""/>            
                 <b>{{info.name}}</b>
               </div>
             <message :id="info.id.toString()"></message>
           </div>
          </vx-card>
      </div>
    </div>

<script>
infos: [],
...
created () {
  this.$http.get('/infos')                                           
    .then((response) => { this.infos = response.data })
    .catch((error) => { console.log(error) })
   
</script>

I believe I updated all of the hard coded values and also I converted info id to String as you requested.

Let me know pls :)



来源:https://stackoverflow.com/questions/65697545/how-do-i-use-the-id-coming-from-the-api-response-in-other-parts-of-html-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!