【问题标题】:How to tell Vuejs to look in a child data entry如何告诉 Vuejs 查看子数据条目
【发布时间】:2020-10-06 10:44:31
【问题描述】:

我正在构建一个网站,将我的数据保存在 .js 文件中,并通过 mapstate 将其导入我的 vuejs 文件中。

我的产品部分有两张相邻的卡片。我目前的问题是,在我的 .js 文件中,我希望 vue 在数据对象中查找数据对象,因此如果有超过 2 个对象,那么它将创建第三张卡片。

这是我用来调用数据的 .js 文件中的产品部分。

"products": [{
                // Everything to do with the Prodcuts section

                //Page heading
                "pageHeading": "OUR PRODUCTS",

                ProductCards: [
                    //Everything to do with the first card
                    {
                        "firstCardImage": "assets/companyname.png",
                        //Heading text 
                        "firstCardHeading": "Card heading text here",
                        //The Card body text 
                        "firstCardTextFirstSection": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum maiores modi quidem veniam, expedita quis laboriosam, ullam facere adipisci, iusto, voluptate sapiente corrupti asperiores rem nemo numquam fuga ab at. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum maiores modi quidem veniam, expedita quis laboriosam, ullam facere adipisci, iusto, voluptate sapiente corrupti asperiores rem nemo numquam fuga ab at.",
                        "firstCardTextSecondSection": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum maiores modi quidem veniam, expedita quis laboriosam, ullam facere adipisci, iusto, voluptate sapiente corrupti asperiores rem nemo numquam fuga ab at.",
                        firstCardButton: "CLICK HERE TO FIND OUT MORE",
                        "firstCardButtonColor": "lightbrwn",
                    }, {
                        //Everything to do with the second card
                        "secondCardImage": "assets/companyname.png",
                        //Heading text 
                        "secondCardHeading": "Card heading text here",
                        //The Card body text 
                        "secondTextFirstSection": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum maiores modi quidem veniam, expedita quis laboriosam, ullam facere adipisci, iusto, voluptate sapiente corrupti asperiores rem nemo numquam fuga ab at. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum maiores modi quidem veniam, expedita quis laboriosam, ullam facere adipisci, iusto, voluptate sapiente corrupti asperiores rem nemo numquam fuga ab at.",
                        "secondCardTextSecondSection": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum maiores modi quidem veniam, expedita quis laboriosam, ullam facere adipisci, iusto, voluptate sapiente corrupti asperiores rem nemo numquam fuga ab at.",
                        SecondCardButton: "CLICK HERE TO FIND OUT MORE",
                        "SecondCardButtonColor": "lightbrwn"
                    }
                ]
            },

        ],

我的 vuejs 文件中的我的产品部分。

  <template >
  <div class="grey lighten-2 py-10 pa-12">
    <div v-for="(item, i) in products" :key="i">
      <div class="py-2">
        <h1 class="text-center font-weight-bold display-2">
          {{ item.pageHeading }}
        </h1>
      </div>
      <v-container>
        <v-row justify="center" align="center">
          <v-col>
            <div data-aos="fade-right">
              <v-hover v-slot:default="{ hover }" open-delay="200">
                <v-card
                  :elevation="hover ? 16 : 0"
                  class="mx-auto"
                  max-width="868.5"
                >
                  <v-row align="center" justify="center"
                    ><v-col cols="4">
                      <v-img
                        width="250"
                        contain
                        background-position="center"
                        :src="cardImages(item.ProductCards.firstCardImage)"
                      >
                      </v-img
                    ></v-col>
                  </v-row>
                  <v-row align="center" justify="center">
                    <h3>{{ item.ProductCards.firstCardHeading }}</h3>
                    <v-col class="text-center" cols="10">
                      <p>
                        {{ item.ProductCards.firstCardTextFirstSection }}
                      </p>

                      <p>
                        {{ item.ProductCards.firstCardTextSecondSection }}
                      </p>
                    </v-col>
                  </v-row>
                  <v-row
                    ><v-col>
                      <div class="ma-5">
                        <v-btn
                          dark
                          tile
                          depressed
                          :color="item.firstCardButtonColor"
                        >
                          <span>
                            {{ item.ProductCards.SecondCardButton }}</span
                          ></v-btn
                        >
                      </div>
                    </v-col>
                  </v-row>
                </v-card>
              </v-hover>
            </div>
          </v-col>
          <v-col>
            <div data-aos="fade-left">
              <v-hover v-slot:default="{ hover }" close-delay="200">
                <v-card
                  :elevation="hover ? 16 : 0"
                  class="mx-auto"
                  max-width="868.5"
                >
                  <v-row align="center" justify="center"
                    ><v-col cols="4">
                      <v-img
                        width="250"
                        contain
                        background-position="center"
                        :src="cardImages(item.ProductCards.secondCardImage)"
                      >
                      </v-img
                    ></v-col>
                  </v-row>
                  <v-row align="center" justify="center">
                    <h3>{{ item.ProductCards.secondCardHeading }}</h3>
                    <v-col class="text-center" cols="10">
                      <p>
                        {{ item.ProductCards.secondTextFirstSection }}
                      </p>

                      <p>
                        {{ item.ProductCards.secondCardTextSecondSection }}
                      </p>
                    </v-col>
                  </v-row>
                  <v-row
                    ><v-col>
                      <div class="ma-5">
                        <v-btn
                          dark
                          tile
                          depressed
                          :color="item.ProductCards.SecondCardButtonColor"
                        >
                          <span>
                            {{ item.ProductCards.SecondCardButton }}</span
                          ></v-btn
                        >
                      </div>
                    </v-col>
                  </v-row>
                </v-card>
              </v-hover>
            </div>
          </v-col>
        </v-row>
      </v-container>
    </div>
  </div>
</template>

目前,当我按照上面的方式进行操作时,它根本没有显示卡片。

编辑*** 我只复制了一小部分而不是整个内容。我今天的大脑还在沉睡。。添加了整个部分

如果有人能启发我的愚蠢,那就太好了。

【问题讨论】:

  • 在代码 sn-p 中根本看不到任何 v-for,所以你错过了循环。
  • 如果您的数据是products: { ProductCards: [{...}, {...}] }item.ProductCards.firstCardImage 与此数据有何关系?至于在数据对象中查找数据对象 ...哪个数据对象将包含这个其他数据对象?这个另一个对象会是什么样子?
  • @Anuga 抱歉,我只复制了文件的一部分而不是整个页面。傻我
  • @JaromandaX 这个想法是主对象控制页面上的所有内容。喜欢产品部分的页面标题和背景,但我不希望将其应用于卡片。在来这里之前我玩过它并得出了一个非常接近的解决方案,您可以在其中添加另一个数据条目,它会自动创建第三张卡片,但它会为每个数据对象一遍又一遍地迭代整个部分。
  • 一切都好,我们都有那些日子 - 孤立的疯狂:p

标签: javascript vue.js


【解决方案1】:

你需要在 v-card 属性中使用 v-for。

 <v-container>
        <v-row justify="center" align="center">
          <v-col>
            <div data-aos="fade-right">
              <v-hover v-slot:default="{ hover }" open-delay="200">
                <v-card
                  v-for="(product, index) in item.ProductCards"
                  :key="index"
                  :elevation="hover ? 16 : 0"
                  class="mx-auto"
                  max-width="868.5"
                >
                 // your template codes go here.
                 // instead of item.ProductCards you should use product
                 // f.e. item.ProductCard.firstCardImage should be product.image
                 // So you need to change the key names to image, heading, etc in ProductCards object
                </v-card>
               // close other tags.

提示:不要使用索引作为键,而是将 id 添加到每个产品并设置该属性:key="product.id"。在可能的情况下,这是更好的做法,在您的情况下也是如此。通过这样做,您可以获得更好的功能(因为您可以使用其 id 获取该特定产品),并且您可以在需要时添加转换组。

【讨论】:

  • v-for 中可以有 v-for 吗?
  • 如果你的意思是
    ...在这里使用 a、b、c
    是的。
  • 我试过了,但它仍然在控制台中给我一个错误 Error in render: "Error: Cannot find module './undefined'" 但是如果我在我的主 v-for v-for="(item, i) in products &amp;&amp; ProductCards" :key="i" 中执行此操作,它会呈现卡片但不会彼此相邻并且我的标题不见了
  • 什么是“products && ProductCards”应该是“products.productsCard”。如果它不起作用,你可能有一个错字。你如何导入你的模块,让它抛出“./undefined”错误?
  • 谢谢,伙计,让它工作。我将 v-for 在您的示例中放置在 &lt;v-cols&gt; 中,并创建了自己的数据对象,而不是在对象内
猜你喜欢
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 2018-09-06
  • 2013-11-30
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 2012-02-10
相关资源
最近更新 更多