【问题标题】:Dynamically switching between lists to display Vuejs?在列表之间动态切换以显示 Vuejs?
【发布时间】:2020-02-16 08:15:21
【问题描述】:

我需要能够在列表之间切换以在 vuejs 中显示。就像这样:

  <app-card
        v-for="card in cardsCache[currentCategory]"
        :key="card"
        :affiliateLink="card.affiliateLink"
        :author="card.author"
        :category="card.category"
        :comments="card.comments"
        :description="card.description"
        :downvotes="card.downvotes"
        :id="card.id"
        :imageUrl="card.imageUrl"
        :timeCreated="card.timeCreated"
        :title="card.title"
        :upvotes="card.upvotes"
        @click-card="clickCard"
        @click-upvote="clickUpvote"
        @click-downvote="clickDownvote"
        @click-affiliateLink="clickAffiliateLink"
        @click-comments="clickComments"
        @click-save="clickSave"
      ></app-card>

注意我正在使用 vfor 做什么,我正在使用可能随时更改的 currentCategory 变量访问一个对象。这种工作,但有问题。当我最初切换类别时,它将按预期呈现,但如果我继续添加到最近切换到类别的数组中,它将不会继续呈现以呈现正在添加到列表中的内容。是的,我检查了列表实际上是添加了项目。我发现任何作为文字添加到 cardsCache 对象的列表,所以:cardsCache: {"popular": []} 那么该列表不会发生此问题。我还发现,如果我在同一个列表中来回切换,它会突然呈现添加到其中的所有新项目。就像 vuejs 无法观察对象一样。我认为我正在尝试做的是相当基本的,但我无法找到任何人这样做的例子。

下面是我如何添加要显示的数据:

      const self = this;
      if (Util.nullOrUndefined(self.cardsCache[category])) {
        self.cardsCache[category] = [];
      }
      if (self.previousCategory != self.currentCategory) {
        Util.scrollToTop();
      }

      if (this.loadingCards === false) {
        if (category == "search") {
          this.cardSearch();
        } else {
          this.loadingCards = true;
          ApiCalls.loadCardsFromCategory(
            this,
            category,
            resp => {
              let cards = resp.data.resp.api_data.cards;
              parseCards(cards).forEach(c => {
                self.cardsCache[category].push(c);
              });

              self.previousCategory = self.currentCategory;
              self.currentCategory = category;
              self.loadingCards = false;
            },
            (error, context) => {
              console.log(error);
              self.loadingCards = false;
            },
            this.userId
          );
        }
      }
    },```

You'll want to pay attention to the line where I say ```self.cardsCache[category].push(c)```


【问题讨论】:

  • 查看vues动态组件,可以通过is prop有条件地渲染组件。
  • 谢谢我去看看
  • 我认为这不能解决我的问题
  • 问题在于 vuejs 无法正确查看 currentCategory 何时更改,问题在于 vuejs 无法正确查看和呈现新添加和/或修改的列表。

标签: vue.js


【解决方案1】:

在我将更多数据加载到缓存中后,将self.$forceUpdate() 添加到部分中,这会迫使 vue 重新呈现解决问题的列表。

【讨论】:

  • 这并不能真正解决根本问题,您只是在修补裂缝,它们会回来导致更多问题。我怀疑真正的问题只是你违反了反应性警告之一,vuejs.org/v2/guide/list.html#Object-Change-Detection-Caveats。对于提供的代码,我猜想self.cardsCache[category] = [] 需要更改为self.$set(self.cardsCache, category, [])。这应该保持一切反应,Vue 将自动进行渲染更新。
【解决方案2】:

有几种方法可以解决您的问题。其中之一可能是使用computed properties

{
  data() {
    return { 
      currentCategory: 'animals',
      cardsCache: {},
    };
  },
  computed: {
    currentCards: function () {
      return this.cardsCache[this.currentCategory];
    }
  },
  ...
}

问题也可能是由于在您的v-for 中不正确使用:key attribute 引起的 - 它应该是数字或字符串,而不是对象 - 类似于 card.id

【讨论】:

  • 谢谢,我会看看密钥,我确实尝试了计算属性,但它有同样的问题。
  • 可悲的是,这似乎并没有改变什么。
  • 是的,我使用 card.id 作为键,它应该是唯一的。但是我认为正在呈现的各种列表可能包含一些相同的卡片,因此也包含一些相同的 id,但我认为这并不重要。
  • 所以问题可能不在于您的 Vue 代码,而在于您填充 cardsCache 的方式
  • 我的意思是当我执行控制台日志并且没有收到控制台错误时,我可以看到大量数据进入缓存。你认为这不是在 vue 中呈现的正确方式吗?
猜你喜欢
  • 2018-03-25
  • 2016-01-14
  • 2012-03-22
  • 2013-11-12
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
相关资源
最近更新 更多