gpt4 book ai didi

javascript - Vue3 与 Vuex4 : v-for only rendering a few -- but not all -- items in an array

转载 作者:行者123 更新时间:2023-12-04 08:45:57 26 4
gpt4 key购买 nike

首次用户和初学者开发人员在这里 - 提前感谢您的帮助!
所以,我正在制作一个简单的 Web 应用程序,它从 API 获取 http JSON 响应,并呈现一个更好看的结果列表。然而,出于某种原因,当使用 v-for 指令渲染项目时,只会显示响应数组中的前几个结果。这是我的 main.js:

import { createApp } from 'vue';
import { createStore } from 'vuex';

import App from './App.vue'
import Axios from "axios";

const store = createStore({
state(){
return {
searchResults: null,
}
},
getters: {

},
mutations: {
setResults(state, payload){
state.searchResults = payload.claims;
},
clearResults(state){
state.searchResults = null;
}
},
actions: {
submitSearch(context, payload) {
context.commit('clearResults');
Axios.request({
baseURL: "https://factchecktools.googleapis.com/v1alpha1/claims:search",
method: "get",
params: {
languageCode: "en",
query: payload.searchTerm,
pageSize: 100,
key: "AIzaSyCqYStCPuaamvXv1qcuWeof0pEx8TguXeY",
},
})
.then((response) => {
context.commit('setResults', {claims: response.data.claims})
//this.$store.dispatch('setResults', {result: response.data.claims})
//this.searchResults = response.data.claims;
//this.$emit("search-results", this.searchResults);
})
.catch((error) => {
console.log(error);
});
},

},
})

这是我的 app.vue:
<template>
<div class="container">
<ClaimSearch @search-results="setResults" />
<ResultCard
v-for="(result, index) in searchResults"
:key="index"
:claim="result.text"
:claimant="result.claimant"
:date="result.claimDate"
:reviews="result.claimReview"
/>
</div>
</template>

<script>
import ClaimSearch from "./components/ClaimSearch.vue";
import ResultCard from "./components/ResultCard.vue";

export default {
name: "App",
components: {
ClaimSearch,
ResultCard,
},
computed: {
searchResults(){
return this.$store.state.searchResults;
}
},
};
</script>
这是我的 ClaimSearch.vue 组件:
<template>
<form @submit.prevent="submitSearch" class="searchBar">
<input type="text" v-model="searchTerm" />
<button type="submit">Is it true?</button>
</form>
</template>

<script>
export default {
data() {
return {
searchTerm: null,
};
},
computed: {},
methods: {
submitSearch(){
this.$store.dispatch('submitSearch', {searchTerm: this.searchTerm})
}
},
};
</script>

最后,这是我的 ResultCar.vue 组件:
<template>
<div class="resultCard">
<div class="claimCard">
<h3>The Claim:</h3>
<p>{{ claim }} - {{ claimant }}, on {{ date.slice(0, 10) }}</p>
</div>

<div class="checkCard">
<h3>Fact Check:</h3>
<hr />

<div v-for="review in reviewList" :key="review.url">
<b>{{ review.publisher.name }}</b>
rated this claim as "<b>{{ review.textualRating }}</b
>". <br />
Source: "<a :href="review.url" target="_blank"> {{ review.title }} </a>"
<br />
- published {{ review.reviewDate.slice(0, 10) }}. <br />
<hr />
</div>
</div>
</div>
</template>

<script>
export default {
props: ["claim", "claimant", "date", "reviews"],
data() {
return {};
},
computed: {
reviewList() {
return this.reviews;
},
},
};
</script>
使用上面的代码,当 API 调用获得一个包含 100 个项目的数组时,只会显示前五或六个——而有些查询根本不显示。是什么赋予了?
再次提前感谢 - 我知道 super 菜鸟!

最佳答案

好吧,我通过简单地构建和部署应用程序解决了这个问题。我猜在 Vue 上发生了一些事情,在本地开发服务器上测试时 v-for 函数中断了?
不管怎么说,还是要谢谢你!

关于javascript - Vue3 与 Vuex4 : v-for only rendering a few -- but not all -- items in an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64327831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com