动态渲染dom

最近在做类似论文生成的一个系统,比较复杂,简单来说就是文字、图表的展示,但是顺序不固定,所以有动态渲染dom的需求,以下是我写的小demo,以作记录。

<template>
    <div id="app">
        <div v-for="item in orderedDivs" :key="item.id" :class="item.class">
            <!-- 内容可以根据需要动态插入 -->
            {{ item.content || '这里是内容区' }}
        </div>
        <el-button @click="handle">随机打乱</el-button>
    </div>
</template>

<script>
export default {
    name: 'App',
    data() {
        return {
            order: ['content', 'table', 'image'],
            divsData: [
                { id: 'content', class: 'content', content: '内容部分' },
                { id: 'image', class: 'img', content: '图片部分' },
                { id: 'table', class: 'table', content: '表格部分' },
            ],
            orderedDivs: [],
        };
    },
    activated() {
        console.log('activated');
        (this.order = ['table', 'image', 'content']), this.getData();
    },
    methods: {
        getData() {
            console.log('this.order');
            this.orderedDivs = this.order.map(id => this.divsData.find(div => div.id === id));
        },
        handle() {
            this.order.sort(() => this.getRandomInt(-1, 1));
            this.getData()
        },
        getRandomInt(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        },
    },
};
</script>

<style scoped>
/* 样式可以根据需要进行调整 */
.content,
.img,
.table {
    height: 100px;
    width: 100px;
    margin-bottom: 20px;
}
.content{
  background-color: aquamarine;
}
.img{
  background-color: beige;
}
.table{
  background-color: lightpink;
}
</style>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值