<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>index</title> </head> <body> <template id="child"> <p>this is child template</p> </template> <template id="parent"> <p>this is parent template</p> <child></child> <child></child> </template> <div id="app"> <parent></parent> </div> <script src="vue.js"></script> <script> var childComponent = Vue.extend({ template: '#child' }); Vue.component("parent",{ template: '#parent', components: { 'child': childComponent, } }); var app = new Vue({ el: '#app' }); </script> </body> </html>
方法2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>index</title> </head> <body> <div id="app"> <parent></parent> </div> <script src="vue.js"></script> <script> var childComponent = Vue.extend({ template: '<p>this is child template</p>' }); Vue.component("parent",{ template: '<p>this is parent template</p><child></child><child></child>', components: { 'child': childComponent, } }); var app = new Vue({ el: '#app' }); </script> </body> </html>