const cnp2 = Vue.extend({ template: ` <div> <h2>我是构造器2</h2> <cpn1></cpn1> </div> `, components: { cpn1: cnp1 } })
const cnp1 = Vue.extend({ template: ` <div> <h2>我是构造器1</h2> </div> ` })
当组件存在这种关系的时候,就存在父子关系--------当一个组件在另外一个组件中注册,这时候,被注册的组件是子组件,包含着子组件的就是父组件 html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/vue.js"></script> </head> <div id="app"> <cpn2></cpn2> </div> <body> <script> const cnp1 = Vue.extend({ template: ` <div> <h2>我是构造器1</h2> </div> ` }) const cnp2 = Vue.extend({ template: ` <div> <h2>我是构造器2</h2> <cpn1></cpn1> </div> `, components: { cpn1: cnp1 } }) let vm = new Vue({ el: '#app', data: () => ({}), components: { cpn2: cnp2 } }) </script> </body> </html>
一、全局组件语法糖vue
Vue.component('mycpn', { template: ` <h2>全局组件,语法糖写法</h2> ` })
二、局部组件语法糖app
components: { mycpn: { template: ` <h2>局部组件,语法糖写法</h2> ` } }
<html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/vue.js"></script> </head> <div id="app"> <mycpn /> </div> <div id="app2"> <mycpn /> </div> <body> <script> Vue.component('mycpn', { template: ` <h2>全局组件,语法糖写法</h2> ` }) let vm = new Vue({ el: '#app', components: { mycpn: { template: ` <h2>局部组件,语法糖写法</h2> ` } }, }) let vm2 = new Vue({ el: '#app2' }) </script> </body> </html>