请教前端大佬一个 Vue.js 问题

356 天前
 imherer

不会前端,最近用现成的框架改点东西,我想通过 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>
      

这个应该怎么写呢

1356 次点击
所在节点    程序员
10 条回复
chensuiyi
356 天前
如果可以,能不能不用 h 函数实现呢。。
shakukansp
356 天前
h('div', arrList.map( i => h('h1', { style:'font-size:30px' }, [i]) ))
imherer
356 天前
@chensuiyi emmm ,谢谢你提醒,我好像知道怎么做了。我试试
imherer
356 天前
@shakukansp 感谢大佬,这个是可行的
lianyue
356 天前
```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>
```
xinfushuo123
356 天前
如果可以用 jsx 的话 试试 v-for
Laobai
356 天前
v-for 直接写在 h1 标签上就行了吧,对了记得加 key
anonydmer
356 天前
虽然技术上没有问题,但是从语义上来说不应该包含多个 h1 吧
waiaan
356 天前
vue 不是支持 jsx 语法吗?
Yukiteru
356 天前
这个没必要用渲染函数,是基础中的基础,直接 v-for 循环渲染就好了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/956479

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX