亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue 創(chuàng)建組件的兩種方法小結(jié)(必看)

 更新時(shí)間:2018年02月23日 14:28:39   作者:cofecode  
Vue 創(chuàng)建組件的方法有哪些呢?下面小編就為大家分享一篇Vue 創(chuàng)建組件的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

創(chuàng)建組件的兩種方法小結(jié)

1.全局注冊(cè)

2.局部注冊(cè)

var child=Vue.extend({})
var parent=Vue.extend({})

Vue.extend() 全局方法 生成構(gòu)造器,創(chuàng)建子類

使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個(gè)“子類”。

這樣寫非常繁瑣。于是vue進(jìn)行了簡(jiǎn)化

使用Vue.component()直接創(chuàng)建和注冊(cè)組件:

Vue.component(id,options) 全局方法 用來(lái)注冊(cè)全局組件

id 是string類型,即是注冊(cè)組件的名稱

options 是個(gè)對(duì)象

// 全局注冊(cè),my-component1是標(biāo)簽名稱
Vue.component('my-component1',{
 template: '<div>This is the first component!</div>'
})
var vm1 = new Vue({
 el: '#app1'
})

Vue.component()的第1個(gè)參數(shù)是標(biāo)簽名稱,第2個(gè)參數(shù)是一個(gè)選項(xiàng)對(duì)象,使用選項(xiàng)對(duì)象的template屬性定義組件模板。

使用這種方式,Vue在背后會(huì)自動(dòng)地調(diào)用Vue.extend()。

在選項(xiàng)對(duì)象的components屬性中實(shí)現(xiàn)局部注冊(cè):

var vm2 = new Vue({
 el: '#app2',
 components: {
  // 局部注冊(cè),my-component2是標(biāo)簽名稱
  'my-component2': {
   template: '<div>This is the second component!</div>'
  },
  // 局部注冊(cè),my-component3是標(biāo)簽名稱
  'my-component3': {
   template: '<div>This is the third component!</div>'
  }
 }
})

==局部注冊(cè)都放在選項(xiàng)對(duì)象中創(chuàng)建==

注意:這里是components,里面可以定義多個(gè)組件。

簡(jiǎn)化后是這樣的寫法

<body>
 <div id='box'>  
  <parent>  
  </parent>
 </div>
 <script src='js/vue.js'></script>
 <script>
  Vue.component('parent',{
   template:`<div><h1>我是父組件</h1><child></child></div>`,
    components:{
    'child':{
     template:`<h1>我是子組件</h1>`
    }
   }
  })
  new Vue({
   el:'#box'
  })
 </script>
</body>

注冊(cè)一個(gè)parent的父組件。然后在父組件的選項(xiàng)對(duì)象中注冊(cè)一個(gè)child的子組件。將子組件child的標(biāo)簽寫到父組件parent的template里面。

頁(yè)面上的樣式結(jié)構(gòu)就是

<div>
 <h1>我是父組件</h1>
 <h1>我是子組件</h1>
</div>

注意:用局部注冊(cè)的子組件不能單獨(dú)直接使用!

標(biāo)簽掛在div里,會(huì)報(bào)錯(cuò)

<div id='box'>  
 <child></child>
</div>

結(jié)果會(huì)報(bào)錯(cuò)。

以上這篇Vue 創(chuàng)建組件的兩種方法小結(jié)(必看)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論