使用Vuex和Nuxt JS查询NewsAPI时,V-for循环不工作

rbl8hiat  于 2023-01-02  发布在  Vue.js
关注(0)|答案(1)|浏览(146)

我最近在Nuxt JS中开始了这个项目,我所要做的就是NewsAPI.org使用Axios和Vuex从www.example.com获取数据,下面显示了代码。

新闻.js

const config = {
  headers: {
    "X-Api-Key": "46afb473359e4504b833abf4cb8ef172"

  }
}

const endPoint = "https://newsapi.org/v2/top-headlines?country=us"

const state = {
  news: [],
  filteredNews: [],
};

const getters = {};

const mutations = {
  SET_NEWS(state, news) {
    state.news = news
  },
};

const actions = {
  // The News list API 
  async newsList({
    commit
  }) {
    try {
      const response = await this.$axios.$get(endPoint, config)
      const news = response.articles
      commit('SET_NEWS', news)
      console.log(news)
    } catch (e) {
      console.log(e)
    }
  },
};

export default {
  state,
  getters,
  mutations,
  actions,
}

它运行良好,我终于能够在控制台中获得数据,只是在news.vue组件中迭代数据,我得到-

新闻视频

<template>
 <section class="text-gray-600 bg-gray-50 font-body">
   <div  class="container px-5 py-24 mx-auto" >
     <div class="grid grid-cols-1 xl:grid-cols-3 gap-12"  >
       <div  class=""  v-for="article in news">
         <div  class="h-full border-2 border-gray-200 border-opacity-60 rounded-lg overflow-hidden">
           <img class="lg:h-48 md:h-36 w-full object-cover object-center" src="https://dummyimage.com/722x402" alt="blog">
           <div class="p-6">
             <h2 class="tracking-widest text-xs title-font font-medium text-gray-800 mb-1">{{article}}</h2>
             <h1 class="title-font text-lg font-medium text-gray-900 mb-3">Shooting Stars</h1>
             <p class="leading-relaxed mb-3">Photo booth fam kinfolk cold-pressed sriracha leggings jianbing microdosing tousled waistcoat.</p>
             <div class="flex items-center flex-wrap ">
               <a class="text-green-500 inline-flex items-center md:mb-2 lg:mb-0">
                 Learn More
                 <svg class="w-4 h-4 ml-2" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
                   <path d="M5 12h14"></path>
                   <path d="M12 5l7 7-7 7"></path>
                 </svg>
               </a>
               <span class="text-gray-400 mr-3 inline-flex items-center lg:ml-auto md:ml-0 ml-auto leading-none text-sm pr-3 py-1 border-r-2 border-gray-200">
                 <svg class="w-4 h-4 mr-1" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
                   <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                   <circle cx="12" cy="12" r="3"></circle>
                 </svg>
                 1.2K
               </span>
               <span class="text-gray-400 inline-flex items-center leading-none text-sm">
                 <svg class="w-4 h-4 mr-1" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
                   <path d="M21 11.5a8.38 8.38 0 01-.9 3.8 8.5 8.5 0 01-7.6 4.7 8.38 8.38 0 01-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 01-.9-3.8 8.5 8.5 0 014.7-7.6 8.38 8.38 0 013.8-.9h.5a8.48 8.48 0 018 8v.5z"></path>
                 </svg>
                 6
               </span>
             </div>
           </div>
         </div>
       </div>
     </div>
   </div>
 </section>
</template>

<script>
import { mapState } from 'vuex'
export default {
 computed: {
   ...mapState(['news'])
 },
 created(){
   this.$store.dispatch('newsList')
 }
}
</script>

最后,这里是我尝试从API添加字段时得到的结果。

yhxst69z

yhxst69z1#

哪一个是正确的?article本身是一个对象(在您的情况下,它看起来像一个数组)。您试图将article作为一个整体呈现,而不是试图呈现article对象内部的一个属性。
例如-不要呈现{{article}},而是尝试{{article.author}}或{{article.title}}

相关问题