gpt4 book ai didi

javascript - 使用 v-if Vue 在 v-for 中添加一些逻辑

转载 作者:行者123 更新时间:2023-12-05 08:50:13 27 4
gpt4 key购买 nike

我有一些带有以下结果的 json

{
"module": [
{
"id": 1,
"title": "Module 1",
"type": "URL",
"size": 1,
"image": "https://localhost/image1.png"
},
{
"id": 2,
"title": "Module 2",
"type": "YOUTUBE",
"size": 2,
"image": "https://localhost/image2.png"
}
]
}

现在我想在带有一些循环和条件的页面上呈现它,如下所示

<template>
<section class="page-section homescreen mt-4">
<div class="container">
<div class="row">
<div class="col-lg-3" v-bind:key="data.index" v-for="data in modules" v-if="data.size == 1">
<img class="img-fluid" :src="`${data.image}`" :alt="`${data.title}`" />
</div>
<div class="col-lg-6" v-bind:key="data.index" v-for="data in modules" v-if="data.size == 2">
<img class="img-fluid" :src="`${data.image}`" :alt="`${data.title}`" />
</div>
</div>
</div>
</section>
</template>

<script>
export default {
data() {
return {
modules: [
{
"id": 1,
"title": "Module 1",
"type": "URL",
"size": 1,
"image": "https://localhost/image1.png"
},
{
"id": 2,
"title": "Module 2",
"type": "YOUTUBE",
"size": 2,
"image": "https://localhost/image2.png"
}
]
};
}
};
</script>

但我没有成功,而是收到了一些错误提示

5:99 error The 'modules' variable inside 'v-for' directive should bereplaced with a computed property that returns filtered array instead.You should not mix 'v-for' with 'v-if' vue/no-use-v-if-with-v-for
8:99 error The 'modules' variable inside 'v-for' directive should bereplaced with a computed property that returns filtered array instead.You should not mix 'v-for' with 'v-if' vue/no-use-v-if-with-v-for

实际上我想创建 <div class="col-lg-3">部分基于 json 是动态的,如果 size:1意思是有col-lg-3如果size:2意思是有col-lg-6

任何解释和建议将不胜感激。谢谢

最佳答案

eslint-plugin-vue告诉你这样做:

<div class="col-lg-3" v-bind:key="data.index" v-for="data in modules.filter(o=>o.size == 1)">
<img class="img-fluid" :src="`${data.image}`" :alt="`${data.title}`" />
</div>
<div class="col-lg-6" v-bind:key="data.index" v-for="data in modules.filter(o=>o.size == 2)">
<img class="img-fluid" :src="`${data.image}`" :alt="`${data.title}`" />
</div>

在你的情况下,它可以简化为

<div :class="'col-lg-'+data.size*3" v-bind:key="data.index" v-for="data in modules">
<img class="img-fluid" :src="`${data.image}`" :alt="`${data.title}`" />
</div>

关于javascript - 使用 v-if Vue 在 v-for 中添加一些逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62568263/

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