vue-component - Vue.js我如何循环抛出一个数组以根据数组项将组件添加到dom
问题描述
我正在制作一个与 api 通信并获取数据的应用程序,主页每天都在变化,所以我不能只向它添加静态组件,我需要根据来自 api 的数据来创建它。
我有一个名为的主页组件Home.vue
这个组件可以有一个或多个轮播,具体取决于我正在获取的数据。
我也有Carousel.vue
负责显示图像的,它有自己的props
。
问题是 :
如何从循环将组件添加到dom
这就是Home.vue
我正在制作的地方loop
:
<template>
<div>
<!--I Need The Loop right here-->
</div>
</template>
<script>
export default {
components: {},
data() {
return {
page_content: [],
widgets: [],
}
},
created() {
this.getHomeContent();
},
methods:
{
getHomeContent() {
window.axios.get(window.main_urls["home-content"]).then(response => {
this.page_content = JSON.parse(JSON.stringify(response.data));
console.log(this.page_content);
for (let index in this.page_content) {
switch (this.page_content[index].type) {
// if type is banner
case 'banner':
switch (this.page_content[index].display) {
// if display is carousel
case 'carousel':
console.log('carousel')
// end if display is carousel
this.widgets.push({
'type': 'Carousel',
'images': this.page_content[index].items,
})
}
// end if type is banner
}
}
});
}
}
}
</script>
这是Carousel.vue
我需要在需要时通过传递道具导入的:
<template>
<div>
<div >
<VueSlickCarousel>
<div v-for="image in images">
<img src="{{img}}">
</div>
</VueSlickCarousel>
</div>
</div>
</template>
<script>
import VueSlickCarousel from 'vue-slick-carousel'
import 'vue-slick-carousel/dist/vue-slick-carousel.css'
import 'vue-slick-carousel/dist/vue-slick-carousel-theme.css'
export default
{
components: {VueSlickCarousel},
name:'Carousel',
props:[
'images'
],
methods:
{
}
}
</script>
如何动态添加Carousel.vue
组件,例如:Home.vue
if(data.display == 'carousel')
{
<carousel images="data.images"></carousel>
}
解决方案
这是正确答案!
<template>
<div>
<template v-for="widget in widgets">
<div v-if="widget.type == 'carousel'" :key="widget.type">
<carousel
:images="widget.images"
:arrows ="widget.arrows"
:dots = "widget.dots"
>
</carousel>
</div>
</template>
</div>
</template>
<script>
import Carousel from './widgets/Carousel.vue'
export default {
components: {Carousel},
data() {
return {
page_content: [],
widgets: [],
}
},
created() {
this.getHomeContent();
},
methods:
{
getHomeContent() {
window.axios.get(window.main_urls["home-content"]).then(response => {
this.page_content = JSON.parse(JSON.stringify(response.data));
console.log(this.page_content);
for (let index in this.page_content) {
switch (this.page_content[index].type) {
// if type is banner
case 'banner':
switch (this.page_content[index].display) {
// if display is carousel
case 'carousel':
console.log('carousel')
// end if display is carousel
this.widgets.push({
'type': 'carousel',
'arrows':true,
'dots':true,
'images': this.page_content[index].items,
})
}
// end if type is banner
}
}
});
}
}
}
</script>
推荐阅读
- windows - 批量重命名多个文件夹中的所有文件
- json - 来自包含双精度数字的 Json 帖子的错误 400(错误请求)
- django - Django-haystack & solr : 增加结果数
- python - UnicodeDecodeError:'utf-8'编解码器无法解码位置 0 的字节 0xff:django 中的无效起始字节
- python-3.x - 根据多列条件过滤熊猫数据框行
- java - 运行 Java 工件时显示 Unicode 字符的问题,但在 IntelliJ IDEA 中运行时一切正常
- jenkins - Jenkins声明性管道负载参数在前提条件下
- javascript - 防止删除 contenteditable div 中的第一段
- javascript - Asp.net Core 中的 ReadAsMultipartAsync
- excel - 将用户表单提交到 Access 数据库时自动增加字母数字字符串