v-for 循环嵌套数组

huangapple go评论69阅读模式
英文:

v-for looping through nested arrays

问题

在列出的元素中,我尝试实现一个v-for循环,它应该迭代script部分中的项目。

HTML代码如下:

<li
   v-for="item in icludedItems.basicPackage"
   class="flex items-center space-x-3">
   <!-- 图标 -->
   <IconsCheck />
   <span>{{ item.basicPackage }}</span>
 </li>

脚本部分看起来像这样:

data() {
    return {
      icludedItems: [
        {
          freePackage: [
            this.$t('pricing.packages.free.includedUsers'),
            this.$t('pricing.packages.free.includedModules'),
            this.$t('pricing.packages.free.includedLocations'),
            this.$t('pricing.packages.free.includedProviders'),
          ],
          basicPackage: [
            this.$t('pricing.packages.basic.includedUsers'),
            this.$t('pricing.packages.basic.includedModules'),
            this.$t('pricing.packages.basic.includedLocations'),
            this.$t('pricing.packages.basic.includedProviders'),
          ],
          advancedPackage: [
            this.$t('pricing.packages.advanced.includedUsers'),
            this.$t('pricing.packages.advanced.includedModules'),
            this.$t('pricing.packages.advanced.includedLocations'),
            this.$t('pricing.packages.advanced.includedProviders'),
          ],
          premiumPackage: [
            this.$t('pricing.packages.premium.includedUsers'),
            this.$t('pricing.packages.premium.includedModules'),
            this.$t('pricing.packages.premium.includedLocations'),
            this.$t('pricing.packages.premium.includedProviders'),
          ],
        },
      ],
    };
  },

然而,采用这种方法不会显示任何内容,但也不会报错。

如果我从includedItems中移除嵌套的数组,它就能按预期工作。

有没有人知道我做错了什么?谢谢 v-for 循环嵌套数组

英文:

In a listed element I'm trying to implement a v-for loop which should iterate the items in the script section.

The HTML code looks like this:

<li
v-for="item in icludedItems.basicPackage"
class="flex items-center space-x-3">
<!-- Icon -->
<IconsCheck />
<span>{{ item.basicPackage }}</span>
</li>

Script sections looks like this:

data() {
return {
icludedItems: [
{
freePackage: [
this.$t('pricing.packages.free.includedUsers'),
this.$t('pricing.packages.free.includedModules'),
this.$t('pricing.packages.free.includedLocations'),
this.$t('pricing.packages.free.includedProviders'),
],
basicPackage: [
this.$t('pricing.packages.basic.includedUsers'),
this.$t('pricing.packages.basic.includedModules'),
this.$t('pricing.packages.basic.includedLocations'),
this.$t('pricing.packages.basic.includedProviders'),
],
advancedPackage: [
this.$t('pricing.packages.advanced.includedUsers'),
this.$t('pricing.packages.advanced.includedModules'),
this.$t('pricing.packages.advanced.includedLocations'),
this.$t('pricing.packages.advanced.includedProviders'),
],
premiumPackage: [
this.$t('pricing.packages.premium.includedUsers'),
this.$t('pricing.packages.premium.includedModules'),
this.$t('pricing.packages.premium.includedLocations'),
this.$t('pricing.packages.premium.includedProviders'),
],
},
],
};
},

However, with this approach no content will be displayed, but also no error.

If I'm removing the nested arrays from includedItems it works as expected.

Anyone a idea what I'm doing wrong? Thanks. v-for 循环嵌套数组

答案1

得分: 1

请尝试以下代码片段。(basicPackageicludedItems 数组中第一个对象的属性)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const app = Vue.createApp({
  data() {
    return {
      icludedItems: [
        {
          freePackage: [
            1,2,3,4
          ],
          basicPackage: [
            5,6,7,8
          ],
          advancedPackage: [
            9,10,11,12
          ],
          premiumPackage: [
            13,14,15,16
          ],
        },
      ],
    };
  },
})
app.mount('#demo')

<!-- language: lang-html -->
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <ul>
    <li v-for="item in icludedItems[0].basicPackage">
      <span>{{ item }}</span>
    </li>
  </ul>
</div>

<!-- end snippet -->

请注意,这是您提供的代码片段的翻译。

英文:

Try like following snippet. (basicPackage is property from the first object in icludedItems array)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const app = Vue.createApp({
data() {
return {
icludedItems: [
{
freePackage: [
1,2,3,4
],
basicPackage: [
5,6,7,8
],
advancedPackage: [
9,10,11,12
],
premiumPackage: [
13,14,15,16
],
},
],
};
},
})
app.mount(&#39;#demo&#39;)

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;demo&quot;&gt;
&lt;ul&gt;
&lt;li v-for=&quot;item in icludedItems[0].basicPackage&quot;&gt;
&lt;span&gt;{{ item }}&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 02:26:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定