У меня есть файл JSON, выводящий следующие стили:
{
"h1" : {
"font-family" : "Lato",
"font-size" : "24px",
"line-height" : "28px",
"font-weight" : 600,
"colorId" : 3,
"margin-bottom" : "10px",
"margin-top" : "20px"
},
"h2" : {
"font-family" : "Lato",
"font-size" : "20px",
"line-height" : "24px",
"font-weight" : 600,
"colorId" : 3,
"margin-bottom" : "10px",
"margin-top" : "20px"
}
}
Я хочу использовать первый ключ «h1, h2, ...» в качестве HTML-тега для его отображения. Возможно ли это с Vue?
Должно получиться примерно так:
<div v-for = "(props, tag) in headers">
<{{ tag }}>x</ {{ tag }}>
</div>






<div v-for = "(props, tag) in headers" v-html = "`<${tag}>x</${tag}>`"></div>
Таким образом, к нему невозможно привязать какие-либо свойства. Вы передаете статическую строку html в v-html. Но вы можете добавить функцию, которая строит html. <div v-for = "(props, tag) в заголовках" v-html = "buildHtml (props, tag)"> </div> buildHtml (props, tag) {return <${tag} + Object.keys (props) .reduce ((total, key) => {return total + `$ {key}: $ {props [key]}"); + > + x + <${tag}>}
Это мило! Спасибо. Как насчет добавления стилей? <div v-for = "(props, tag) в заголовках" v-html = "
<${tag} v-bind:style='${props}'>This is a ${ tag }-header</${tag}>"> </div>