Vue uses native js to implement automatic generation of custom components, vuejs

Source: Internet
Author: User

Vue uses native js to implement automatic generation of custom components, vuejs

For the current three mainstream front-end data driver frameworks (vue, ng, react), all have APIs for creating custom components, but they must first write the mount point in advance, this mount point can be the original static element label or custom template. for multiple components generated through the same data stream, if you write a mount point on the page in advance ), then, you can use the dom operation to dynamically Add a message similar to the following: Failed to execute 'appendchild 'on 'node': parameter 1 is not of type 'node '. (...). Why? What should we do next?

The reason is that any dom operation object must be a W3C-compliant element, unless as described below, rewrite and generate the html Element Object prototype (HTMLElement. prototype) and register custom elements to dynamically generate custom components.

However, we all know that the original intention of using the data-driven framework is to avoid dom operations as much as possible. The following Code still contains some dom operations. For the current level of cognition, it seems that these necessary dom operations cannot be avoided. For more information, see the code...

<! DOCTYPE html> 

To maintain the maintainability and ease of coding, I put the template part in a separate fuhao-components.js file, as shown below:

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"> 

The final rendering effect is as follows:

In view of the fact that vue dynamically generates custom components using dom operations, the console will report certain errors. This bug is still being fixed, it may be necessary to have a deeper understanding of vue Data Binding and transmission mechanisms and js Dynamic Registration of custom components...

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.