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>