您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“Vue结合原生js怎么实现自定义组件自动生成”,在日常操作中,相信很多人在Vue结合原生js怎么实现自定义组件自动生成问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue结合原生js怎么实现自定义组件自动生成”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="Content-type" content="text/html,charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE-edge"> <meta name="viewport" content="width=device-width,initial-scale=1"> <link href="css/mui.min.css" rel="stylesheet"> <link href="css/app.css" rel="stylesheet"> <script src="js/vue.js" type="text/javascript"></script> </head> <body> <div id="main" class="mui-content"> </div> </body> <script src="js/fuhao-components.js" type="text/javascript"></script> <script> var jsonData = [ { "keyname": "姓名鄂然失色而热重重中之重重中之重杂志的热热", "type": "text", "key": "name11" }, { "value": "姓名鄂之重杂志的热热", "key": "name11" }, { "keyname": "姓名鄂然失色而热热热热是重中之重重中之重重中之重杂志的热热", }, { "keyname": "姓名鄂然失色而热热热热是重中之重重中之重重中之重杂志的热热", "type": "textarea", "key": "name" }, { "keyname": "性别", "type": "radio", "key": "sex", "values": [ { "key": "man", "value": "男辅导班" }, { "key": "women", "value": "女" } ] }, { "keyname": "复选", "type": "checkbox", "key": "checkbox", "values": [ { "key": "man", "value": "男" }, { "key": "women", "value": "女" } ] }, { "keyname": "类型", "type": "select", "key": "type1", "values": [ { "key": "type1", "value": "类型1" }, { "key": "type2", "value": "类型2" }, { "key": "type3", "value": "类型3" }, { "key": "type4", "value": "类型4" } ] }, { "keyname": "定位", "type": "gps", "key": "btn", "value": "地图获取定位" }, { "keyname": "拍照", "type": "photo", "key": "btn", "value": "拍照" } ]; (function () { AnalyJson(jsonData); })(); function AnalyJson(data) { if ('id' in data) { arguments.callee(data.values); } else { if ('name' in data) { htmlname = data.name; CreateInputViewer(data.name); arguments.callee(data.values); } else { if ('type' in data) { CreateInputViewer(data); } else { for (var p in data) { CreateInputViewer(data[p]); } } } } } function CreateInputViewer(data) { switch (data.type) { case 'text': { fh_C(data, 'c-input-text' + '-' + data.key, 'fhInputText', textTpl); break; } case 'textarea': { fh_C(data, 'c-textarea' + '-' + data.key, 'fhInputTextarea', textareaTpl); break; } case 'radio': { fh_C(data, 'c-input-radio' + '-' + data.key, 'fhInputTextarea', radioTpl); break; } case 'checkbox': { fh_C(data, 'c-input-checkbox' + '-' + data.key, 'fhInputCheckbox', checkboxTpl); break; } case 'select': { fh_C(data, 'c-select' + '-' + data.key, 'fhSelect', selectTpl); break; } case 'photo': { fh_C(data, 'c-photo' + '-' + data.key, 'fhPhoto', photoTpl); break; } case 'gps': { fh_C(data, 'c-gps' + '-' + data.key, 'fhGPS', gpsTpl); break; } default: { fh_C(data, 'c-default' + '-' + data.key, 'fhInputDefault', defaultTpl); break; } } } function fh_C(d, c, cn, tpl) { console.log(d); Vue.component(c, { template: tpl, // props:['key','keyname','values','value'], data: function () { return d } }); new Vue({ el: '.mui-content', components: { cn: cn }, }); var MyElementProto = Object.create(HTMLElement.prototype); MyElementProto.createdCallback = function () { this.innerHTML = tpl }; var MyComponent = document.registerElement(c, {prototype: MyElementProto}); document.querySelector('.mui-content').appendChild(new MyComponent()); } </script> </html>
为了保持代码的可维护性及易读性,我将模板部分单独放在fuhao-components.js的文件里边,如下所示:
var textTpl=''; <div class="mui-content-padded"> <input :type="type" :name="key" :placeholder="keyname" > </div> var textareaTpl= ''; <div class="mui-content-padded "> <textarea rows="5" :placeholder="keyname"> </textarea> </div> var radioTpl= ''; <form class="mui-input-group mui-content-padded"> <div class="mui-input-row mui-radio mui-left"v-for="value in values"> <label>{{value.key}}</label> <input :name="key" :type="type" :value="key"> </div> </form> var checkboxTpl= ''; <form class="mui-input-group mui-content-padded"> <div class="mui-input-row mui-checkbox mui-left"v-for="value in values"> <label>{{value.key}}</label> <input :name="key" :type="type" :value="key"> </div> </form> var selectTpl= ''; <div class="mui-content-padded"> <h6 class="mui-content-padded" v-text="keyname"></h6> <select class="mui-btn mui-btn-block " :name="key"> <option value="key" v-text="value.key" v-for="value in values">{{value.key}}</option> </select> </div> var photoTpl= ''; <div class="mui-content-padded"> <span v-text="keyname"></span> <button :name="key" onclick="takePhoto(this.name)" class="mui-btn mui-btn-primary">拍照</button> <img :id="key" height="70" width="100" class="img-rounded"> </div> var gpsTpl=''; <div class="mui-content col-xs-12"> <button class="mui-btn mui-btn-primary" :id="key" onclick="takeLocation(this.id)"> 获取定位 </button> </div> var defaultTpl= ''; <div class="mui-content-padded " v-if="key"> <ul class="mui-table-view"> <li class="mui-table-view-cell mui-media"> <span class="fuhaoKey" v-text="key"></span> <span class="fuhaoValue" v-text="value"></span> </li> </ul > </div>
最终渲染效果如下:
到此,关于“Vue结合原生js怎么实现自定义组件自动生成”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。