【发布时间】:2021-05-11 15:45:08
【问题描述】:
美好的一天社区。p>
我需要您的帮助,以便在 VUE 中从 JSON 动态构建 HTML 控件。
目前我完成了一个练习,我有一个 html 模板,它根据 JSON 中设置的参数呈现输入控件列表。但是我想构建更多控件,而不仅仅是输入控件。
export default {
name: 'app',
data () {
return {
msg: 'Hola Mundo',
fields: [
{
"name": "fechaRegistro",
"label": "Fecha de Registro",
"type": "date",
"placeholder": 'Ingresa Fecha'
},
{
"name": "nombreDeUsuario",
"label": "Nombre de Usuario",
"type": "text",
"placeholder": "Ingresa Usuario"
},
{
"name": "passwordUsuario",
"label": "Password",
"type": "password",
"placeholder": "Contraseña"
},
{
"name": "adjuntarArchivo",
"label": "Adjuntar",
"type": "file"
},
{
"name": "activo",
"label": "Activo",
"type": "checkbox"
},
{
"name": "roles",
"label": "Roles",
"type": "multiSelect",
"sortedByKey": false,
"options": [{
"name": "admin",
"label": "Admin"
},
{
"name": "user",
"label": "User"
},
{
"name": "guest",
"label": "Invitado"
}
]
}
]
}
}
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
<template>
<div id="app">
<h1>{{msg}}</h1>
<form>
<ul id="controles">
<li v-for="field in fields" :key="field">
<label :for="field.name">{{field.label}}</label>
<input :id="field.name" :type="field.type" :placeholder="field.placeholder">
</li>
</ul>
</form>
</div>
</template>
我的目标是在 Json 中接收我应该构建的控件类型及其参数。比如json里来了select类型的控件,我必须建一个select,如果来了一个textarea类型的控件,我必须建一个textarea等等。
请留下您的解决方案,谢谢。
【问题讨论】:
标签: javascript html vue.js templates components