axios $route.params.id不想在点击饮料时给予id数据,CocktailDB API,nuxt.js

czq61nw1  于 2022-12-12  发布在  iOS
关注(0)|答案(1)|浏览(109)

你好,我正在做一个简单的cocktailapp,用coktailDB API https://www.thecocktaildb.com/api.php来练习nuxt.js和axios。在drinks/index.vue中,我列出了所有带v-for的饮料。当点击一种饮料时,你会进入drinks/_id/index.vue页面,在那里,关于这种饮料的完整信息会通过它的ID显示出来。我使用的是$route.params.id,我已经试过了。$route.params. id。没有饮料信息显示出来。它只是显示未定义和括号。我如何才能得到饮料信息显示后,它的ID点击一个特定的饮料?提前感谢!
饮料/索引值;

<template>
  <div>
  <div>
    <SearchDrink/>
  </div>
  <div>
  <div v-for="drink in drinks" :key="drink.id"> 
  <nuxt-link :to="'drinks/' + id">
    <div class="drink">
    <p> {{ drink.strDrink
    }} </p>
    <img :src="drink.strDrinkThumb" alt=""/> 
    <p>Instructions:</p>
    <p> {{ drink.strInstructions }} </p>
    <div class="ing"> Ingridients: 
      <ul>
        <li>{{ drink.strIngredient1 }} </li>
        <li>{{ drink.strIngredient2 }} </li>
        <li>{{ drink.strIngredient3 }} </li>
        <li>{{ drink.strIngredient4 }} </li>
        <li>{{ drink.strIngredient5 }} </li>
      </ul>
  </div>
  </div>
</nuxt-link>
</div>
</div>
</div>
</template>

<script>
import SearchDrink from '../../components/SearchDrink.vue'
import axios from 'axios'

export default {
  components:{
    SearchDrink,
  },
  data(){
    return {
      drinks: [],
    }
  },
 methods: {
    getAllDrinks(){
      axios.get('https://thecocktaildb.com/api/json/v1/1/search.php?s=')
      .then((response) => {
        this.drinks = response.data.drinks
        const myDrink = response.data.drinks
        console.log(myDrink)
        console.log(myDrink.strDrinkThumb)  
        })
    .catch((error) =>{
      console.log(error)
    })
    
    }, 
 },
  created(){
    this.getAllDrinks()
  },
  // methods: {
  //   searchDrink(){
  //     if(!this.search){
  //       return this.drinks
  //     }else{
  //       return this.drinks.filter(drink => 
  //       drink.text.toLowerCase().includes(this.search.
  //       toLowerCase()))
  //     }
  //   }
  // },
    head(){
        return {
            title: 'Drinks App',
            meta: [
                {
                    hid: 'description',
                    name: 'description',
                    content: 'Best place to search a Drink'
                }
            ]
        }
    }
}
</script>

饮料/_id/索引.vue;

<template>
  <div>
    <nuxt-link to="/drinks">
        Go Back
    </nuxt-link>
    <h2> {{ drink }} </h2>
    <hr>
    <small>Drink ID: {{ this.$route.params.id }}</small>
  </div>
</template>

<script>
import axios from 'axios'

export default {
    data(){
        return{
            drink: {}
        }
    },
    methods: {
    getAllDrinks(){
      axios.get(`www.thecocktaildb.com/api/json/v1/1/lookup.php?i=${this.$route.params.id}`)
      .then((response) => {
        this.drinks = response.data.drinks
        const myDrink = response.data.drinks
        console.log(myDrink) 
        })
    .catch((error) =>{
      console.log(error)
    })
    
    }, 
 },
  created(){
    this.getAllDrinks()
  },
  head(){
        return {
            title: this.drink,
            meta: [
                {
                    hid: 'description',
                    name: 'description',
                    content: 'Best place to search a Drink'
                }
            ]
        }
    }
}
</script>

a64a0gku

a64a0gku1#

这里有很多需要改进的地方(API也有点混乱)。
下面是我如何用现代实践来做这件事。
/pages/drinks/index.vue

<template>
  <div>
    <div v-for="drink in drinks" :key="drink.idDrink">
      <nuxt-link :to="`/drinks/${drink.idDrink}`">
        <div class="drink">
          <p>{{ drink.strDrink }}</p>
          <img width="100px" height="100px" :src="drink.strDrinkThumb" alt="" />
          <p>Instructions:</p>
          <p>{{ drink.strInstructions }}</p>
          <div class="ing">
            <p>Ingredients:</p>
            <ul>
              <li>{{ drink.strIngredient1 }}</li>
              <li>{{ drink.strIngredient2 }}</li>
              <li>{{ drink.strIngredient3 }}</li>
              <li>{{ drink.strIngredient4 }}</li>
              <li>{{ drink.strIngredient5 }}</li>
            </ul>
          </div>
        </div>
      </nuxt-link>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      drinks: [],
    }
  },
  async fetch() {
    await this.getAllDrinks()
  },
  methods: {
    async getAllDrinks() {
      try {
        const { data } = await axios.get(
          'https://thecocktaildb.com/api/json/v1/1/search.php?s='
        )
        this.drinks = data.drinks
      } catch (error) {
        console.log('error', error)
      }
    },
  },
}
</script>

/pages/drinks/_id.vue

<template>
  <div>
    <nuxt-link to="/drinks"> Go Back </nuxt-link>
    <h2>{{ drink.strGlass }}</h2>
    <hr />
    <small>Drink ID: {{ $route.params.id }}</small>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      drink: {},
    }
  },
  async fetch() {
    await this.getAllDrinks()
  },
  methods: {
    async getAllDrinks() {
      try {
        const { data } = await axios.get(
          `https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=${this.$route.params.id}`
        )
        this.drink = data.drinks[0]
      } catch (error) {
        console.log('error', error)
      }
    },
  },
}
</script>

注意事项:

  • 我跳过了无法访问的部分(如SearchDrink组件)或与当前问题无关的部分
  • 您可能需要使用axios module
  • 您不需要导入Nuxt组件,它是done for you

相关问题