【问题标题】:Need help limiting array/optimizing axios responses需要帮助限制数组/优化 axios 响应
【发布时间】:2023-03-10 14:40:02
【问题描述】:

我有类似/url/{category} 的东西。

这是在主页上获取其中一些的代码:

export default {
    data() {
        return {
            topnews:[],
            newsfive:[],
            categories: { 
                tshirts:'',
                shirts:'',
                shoes:'',
                useful:'', 
            }
        }
    },
    methods() {
        async getAll(){
            axios.all([
                axios.get(`/topnews`),
                axios.get(`/news`),
                axios.get(`/tshirts`),
                axios.get(`/shirts`),
                axios.get(`/shoes`),
                axios.get(`/useful`)])
                .then(axios.spread((topnews, news, tshirts, shirts, shoes, useful) => {
                    news.data.length = 5;
                    tshirts.data.length = 5
                    shirts.data.length = 5
                    shoes.data.length = 5
                    useful.data.length = 5
                    // How to limit these appropriately? 

                    this.newsfive = news.data;
                    this.topnews = topnews.data;

                    this.categories = {
                        tshirts: tshirts.data,
                        shirts: shirts.data,
                        shoes: shoes.data,
                        useful: useful.data,
                    }
                })).catch(error => console.log(error))
        }
    }
    created()  {
        this.getAll() 
    }
}

这可行,但如果我将路由更改为 /tshirts 并使用浏览器返回主页,我会得到:

typeerror 内容只读属性

也可以将其组合成一个数组,而不是创建 7 个不同的 div,例如:

<div v-for="tshirts,key in categories.tshirts" :key="categories.tshirts.slug">
    <img :src="tshirts.thumb" class="img-responsive" width=100%/>
    <p>{{tshirts.title}}</p>
</div>

取而代之的是过滤计算的 axios 响应,然后只使用单个 div?

<div v-for="item,key in categories(tshirts)" :key="categories(item.slug)">

如何限制 axios 数组响应大小?

【问题讨论】:

  • 样式、语法和格式。
  • 您收到响应后似乎已经在限制响应大小。与路线更改相关的错误似乎是您要解决的问题。你用的是什么客户端路由器?你能分享一个codesandbox 来重现这个问题吗?
  • 抱歉延迟回复,感谢您的帮助。我解决了它,而不是使用 news.data.length,我按照@Alexander Yakushev 的建议放置了 news.data.slice(0,5)。

标签: javascript laravel vue.js axios


【解决方案1】:

创建Category.vue 以仅呈现类别内容

<template>
  <div v-for="(item, key) in category" :key="item.slug">
      <img :src="item.thumb" class="img-responsive" width=100% />
      <p>{{ item.title }}</p>
  </div>
</template>
<script>
export default {
    data() {
        return {
            category: { }
        }
    },
    methods() {
        getCategory() {
          axios.get(`/${this.$route.params.category}`)                
               .then((response) => {
                  this.category = response.data.slice(0, 5);                    
               }).catch(error => console.log(error));
        }
    }
    created()  {
        this.getCategory() 
    }
}
</script>

App.vue 中添加router-link 到所有类别

<template>
   <nav>
      <router-link to="{ name: 'category', params: {category: 'tshirts'} }">T-Shirts</router-link>
      <router-link to="{ name: 'category', params: {category: 'shirts'} }">Shirts</router-link>
     <!-- and other -->
   </nav>
   <main>
      <router-view></router-view>
   </main
</template>

不要伪造vue-router

import Category from "./Category.vue"

routes = [
  {
    name: "category",
    path: "/:categoryId",
    component: Category
  }
]

【讨论】:

  • 好吧,我需要在主页上获取和显示项目,而不是为指向不同页面的项目创建路由链接,但无论如何 slice(0,5) 完全符合我的要求,我将您提供的代码作为奖励,谢谢。对未来读者的快速说明是,如果没有至少 5 条记录,它将不会显示任何内容 - 显而易见,但值得一提。无论如何,这对我来说不是问题。
  • 是的,我弄错了。应该有slice(0, 5)
猜你喜欢
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
相关资源
最近更新 更多