
不会前端,最近用现成的框架改点东西,我想通过 h 动态渲染一些 html 标签应该如何实现
return h( <div> <h1 style="font-size:30px">1</h1> <h1 style="font-size:30px">2</h1> <h1 style="font-size:30px">3</h1> </div> ) 例如上面的代码,div 里面的 h1 标签数量不确定,是通过一个数组来决定的,类似于这样
const arrList = [1, 2, 3] <div v-for="item in arrList"> <h1 style="font-size:30px">{item}</h1> </div> 这个应该怎么写呢
1 chensuiyi 2023 年 7 月 13 日 如果可以,能不能不用 h 函数实现呢。。 |
2 shakukansp 2023 年 7 月 13 日 h('div', arrList.map( i => h('h1', { style:'font-size:30px' }, [i]) )) |
4 imherer OP @shakukansp 感谢大佬,这个是可行的 |
5 lianyue 2023 年 7 月 13 日 ```html <template> <AppBar></AppBar> </template> <script setup lang="ts"> const arrList = [1, 2, 3] let children = [] for (let i = 0; i < arrList.length; i++) { const item = arrList[i]; children.push(h('h1', { style: 'font-size:30px' }, String(item))) } const AppBar = h('div', {class:'h-div'}, children) </script> ``` |
6 xinfushuo123 2023 年 7 月 13 日 如果可以用 jsx 的话 试试 v-for |
7 Laobai 2023 年 7 月 13 日 v-for 直接写在 h1 标签上就行了吧,对了记得加 key |
8 anonydmer 2023 年 7 月 13 日 虽然技术上没有问题,但是从语义上来说不应该包含多个 h1 吧 |
9 waiaan 2023 年 7 月 13 日 vue 不是支持 jsx 语法吗? |
10 Yukiteru 2023 年 7 月 13 日 这个没必要用渲染函数,是基础中的基础,直接 v-for 循环渲染就好了。 |