Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the automatic Generation of user-defined components by combining Vue with Native js

Shulou Source: shulou.com Published: 2022-05-31 12:07:22 09月26日 Update

This article mainly introduces "Vue combined with native js how to achieve automatic generation of custom components". In daily operation, I believe many people have doubts about how to achieve automatic generation of custom components by combining Vue with native js. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to achieve automatic generation of custom components by combining Vue with native js". Next, please follow the editor to study!

Var jsonData = [{"keyname": "the heat of the magazine is the top priority of the name", "type": "text", "key": "name11"}, {"value": "the heat of the name E Zhizhong magazine", "key": "name11"} {"keyname": "the name disappears and the heat is the heat of the most important magazine", {"keyname": "the name disappears suddenly and the heat is the heat of the top magazine", "type": "textarea", "key": "name", {"keyname": "gender" "type": "radio", "key": "sex", "values": [{"key": "man", "value": "male tutoring class"}, {"key": "women", "value": "female"}]} {"keyname": "check", "type": "checkbox", "key": "checkbox", "values": [{"key": "man", "value": "male"}, {"key": "women", "value": "female"}]} {"keyname": "type", "type": "select", "key": "type1", "values": [{"key": "type1", "value": "type 1"}, {"key": "type2", "value": "type 2"} {"key": "type3", "value": "Type 3"}, {"key": "type4", "value": "Type 4"}]}, {"keyname": "location", "type": "gps", "key": "btn" "value": "Map acquisition location"}, {"keyname": "Photo", "type": "photo", "key": "btn", "value": "Photo"}] (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,' -'-'+ data.key, 'fhInputText', textTpl); break } case 'textarea': {fh_C (data,' Cmurtextarea'+'-'+ data.key, 'fhInputTextarea', textareaTpl); break;} case' radio': {fh_C (data, 'Cmuri' -'+ data.key, 'fhInputTextarea', radioTpl); break } case 'checkbox': {fh_C (data,' data.key,'-'+ data.key, 'fhInputCheckbox', checkboxTpl); break;} case' select': {fh_C (data, 'CME select' +'-'+ data.key, 'fhSelect', selectTpl); break } case 'photo': {fh_C (data,' CMB photo' +'-'+ data.key, 'fhPhoto', photoTpl); break;} case' gps': {fh_C (data, 'CMULG' +'-'+ data.key, 'fhGPS', gpsTpl); break } default: {fh_C (data, 'cmurdefault` +' -'+ 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 () {th [XSS _ clean] = tpl}; var MyComponent = document.registerElement (c, {prototype: MyElementProto}); document.querySelector ('.child) .appendChild (new MyComponent ());}

In order to keep the code maintainable and readable, I put the template section separately in the fuhao-components.js file, as follows:

Var textTpl=''; var textareaTpl=''; var radioTpl=''; {{value.key}} var checkboxTpl=''; {{value.key}} var selectTpl=''; {{value.key}} var photoTpl=''; get location var defaultTpl=''

The final rendering effect is as follows:

At this point, the study on "how to realize the automatic generation of custom components by combining Vue with native js" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Top priority type component automatic generation generation name magazine learning E ran location more help utility next code and ease maintainability map gender effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux Docker Shulou Technology MySQL