vue component

1.入門

<div id="example">
  <my-component></my-component>
</div>
// register 全局
Vue.component('my-component', {
  template: '<div>A custom component!</div>'
})
// create a root instance
new Vue({
  el: '#example'
})

結(jié)果

<div id="example">
     <div>A custom component!</div>
</div>

局部組件

var Child = {
  template: '<div>A custom component!</div>'
}
new Vue({
  // ...
  components: {
    // <my-component> will only be available in parent's template
    'my-component': Child
  }
})

2.陷阱

<ul>, <ol>, <table> and <select>
會(huì)有問題,會(huì)報(bào)錯(cuò)休里。應(yīng)該用is
<table>
    <tr is="my-row"></tr>
</table>

以下三種不起作用
(1)<script type="text/x-template">
(2)JavaScript inline template strings
(3).vue components

3.data必須是function

<div id="example-2">
  <simple-counter></simple-counter>
  <simple-counter></simple-counter>
  <simple-counter></simple-counter>
</div>

var data = { counter: 0 }
Vue.component('simple-counter', {
  template: '<button v-on:click="counter += 1">{{ counter }}</button>',
  // data is technically a function, so Vue won't
  // complain, but we return the same object
  // reference for each component instance
  data: function () {
            return {
                counter: 0
            }
  }
})
new Vue({
  el: '#example-2'
})
Paste_Image.png

4.父子組件 props

props down, events up. 父組件傳遞 data down to the child 通過 props, child sends messages to the parent via events.

Paste_Image.png

(1)props傳值

Vue.component('child', {
  // declare the props
  props: ['message'],
  // just like data, the prop can be used inside templates
  // and is also made available in the vm as this.message
  template: '<span>{{ message }}</span>'
})
<child message="hello!"></child>

(2)動(dòng)態(tài) props

<div>
<input v-model="parentMsg">
<br>
<child v-bind:my-message="parentMsg"></child>
</div>
//簡(jiǎn)寫形式
<child :my-message="parentMsg"></child>


<comp v-bind:some-prop="1"></comp>
傳遞的是數(shù)字1滓窍,非字符串
(3)Prop Validation

Vue.component('example', {
  props: {
    // basic type check (`null` means accept any type)
    propA: Number,
    // multiple possible types
    propB: [String, Number],
    // a required string
    propC: {
      type: String,
      required: true
    },
    // a number with default value
    propD: {
      type: Number,
      default: 100
    },
    // object/array defaults should be returned from a
    // factory function
    propE: {
      type: Object,
      default: function () {
        return { message: 'hello' }
      }
    },
    // custom validator function
    propF: {
      validator: function (value) {
        return value > 10
      }
    }
  }
})

當(dāng)檢查失敗菱农,會(huì)給個(gè)warining醇坝,type選項(xiàng)
String
Number
Boolean
Function
Object
Array

4.父子組件 event

父組件可以監(jiān)聽子組件的事件

<div id="counter-event-example">
  <p>{{ total }}</p>
  <button-counter v-on:increment="incrementTotal"></button-counter>
  <button-counter v-on:increment="incrementTotal"></button-counter>
</div>
Vue.component('button-counter', {
  template: '<button v-on:click="increment">{{ counter }}</button>',
  data: function () {
    return {
      counter: 0
    }
  },
  methods: {
    increment: function () {
      this.counter += 1
      this.$emit('increment')
    }
  },
})
new Vue({
  el: '#counter-event-example',
  data: {
    total: 0
  },
  methods: {
    incrementTotal: function () {
      this.total += 1
    }
  }
})
Paste_Image.png

如果是原生事件+.native

<my-component v-on:click.native="doTheThing"></my-component>

**5.v-model 用于組件

<input v-model="something">
等價(jià)于
<input v-bind:value="something" v-on:input="something = $event.target.value">

//條件
accept a value prop
emit an input event with the new value

<div id="v-model-example">
  <p>{{ message }}</p>
  <my-input
    label="Message"
    v-model="message"
  ></my-input>
</div>
Vue.component('my-input', {
  template: '\
    <div class="form-group">\
      <label v-bind:for="randomId">{{ label }}:</label>\
      <input v-bind:id="randomId" v-bind:value="value" v-on:input="onInput">\
    </div>\
  ',
//1.必須有value
  props: ['value', 'label'],
  data: function () {
    return {
      randomId: 'input-' + Math.random()
    }
  },
  methods: {
    onInput: function (event) {
    //2.必須觸發(fā)input
      this.$emit('input', event.target.value)
    }
  },
})
new Vue({
  el: '#v-model-example',
  data: {
    message: 'hello'
  }
})
Paste_Image.png

6.不是父子組件的交流

創(chuàng)建空的Vue對(duì)象

var bus = new Vue()
// in component A's method
bus.$emit('id-selected', 1)
// in component B's created hook
bus.$on('id-selected', function (id) {
  // ...
})

7.Single slot

父組件的內(nèi)容會(huì)被刪除董习,除非子組件中有多余一個(gè)的slot標(biāo)簽

Suppose we have a component called my-component with the following template:
<div>
  <h2>I'm the child title</h2>
  <slot>
    This will only be displayed if there is no content
    to be distributed.
  </slot>
</div>

And a parent that uses the component:
<div>
  <h1>I'm the parent title</h1>
  <my-component>
    <p>This is some original content</p>
    <p>This is some more original content</p>
  </my-component>
</div>

The rendered result will be:
<div>
  <h1>I'm the parent title</h1>
  <div>
    <h2>I'm the child title</h2>
    <p>This is some original content</p>
    <p>This is some more original content</p>
  </div>
</div>

8.Name slot

For example, suppose we have an app-layout component with the following template:

<div class="container">
  <header>
    <slot name="header"></slot>
  </header>
  <main>
    <slot></slot>
  </main>
  <footer>
    <slot name="footer"></slot>
  </footer>
</div>
Parent markup:

<app-layout>
  <h1 slot="header">Here might be a page title</h1>
  <p>A paragraph for the main content.</p>
  <p>And another one.</p>
  <p slot="footer">Here's some contact info</p>
</app-layout>
The rendered result will be:

<div class="container">
  <header>
    <h1>Here might be a page title</h1>
  </header>
  <main>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
  </main>
  <footer>
    <p>Here's some contact info</p>
  </footer>
</div>

9.動(dòng)態(tài)組件

<component>元素綁定is屬性

var vm = new Vue({
  el: '#example',
  data: {
    currentView: 'home'
  },
  components: {
    home: { /* ... */ },
    posts: { /* ... */ },
    archive: { /* ... */ }
  }
})
<component v-bind:is="currentView">
  <!-- component changes when vm.currentView changes! -->
</component>
If you prefer, you can also bind directly to component objects:

var Home = {
  template: '<p>Welcome home!</p>'
}
var vm = new Vue({
  el: '#example',
  data: {
    currentView: Home
  }
})

如果要緩存所有組件

<keep-alive>
<component :is="currentView">
<!-- inactive components will be cached! -->
</component>
</keep-alive>

10.簡(jiǎn)寫形式

<my-component
:foo="baz" //v-bind
:bar="qux"
@event-a="doThis" //v-on
@event-b="doThat"
>
<img slot="icon" src="...">
<p slot="main-text">Hello!</p>
</my-component>

11.獲取子組件的引用ref

<div id="parent">
<user-profile ref="profile"></user-profile>
</div>

var parent = new Vue({ el: '#parent' })
// access child component instance
var child = parent.$refs.profile

12.X-template

<script type="text/x-template" id="hello-world-template">
<p>Hello hello hello</p>
</script>

Vue.component('hello-world', {
    template: '#hello-world-template'
})
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末领炫,一起剝皮案震驚了整個(gè)濱河市将硝,隨后出現(xiàn)的幾起案子岩四,更是在濱河造成了極大的恐慌哭尝,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剖煌,死亡現(xiàn)場(chǎng)離奇詭異材鹦,居然都是意外死亡逝淹,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門桶唐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來栅葡,“玉大人,你說我怎么就攤上這事尤泽⌒来兀” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵坯约,是天一觀的道長(zhǎng)熊咽。 經(jīng)常有香客問我,道長(zhǎng)闹丐,這世上最難降的妖魔是什么横殴? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮卿拴,結(jié)果婚禮上衫仑,老公的妹妹穿的比我還像新娘。我一直安慰自己堕花,他們只是感情好文狱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著缘挽,像睡著了一般瞄崇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上到踏,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天杠袱,我揣著相機(jī)與錄音,去河邊找鬼窝稿。 笑死楣富,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的伴榔。 我是一名探鬼主播纹蝴,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼踪少!你這毒婦竟也來了塘安?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤援奢,失蹤者是張志新(化名)和其女友劉穎兼犯,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡切黔,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年砸脊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纬霞。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡凌埂,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出诗芜,到底是詐尸還是另有隱情瞳抓,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布伏恐,位于F島的核電站孩哑,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏翠桦。R本人自食惡果不足惜臭笆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望秤掌。 院中可真熱鬧,春花似錦鹰霍、人聲如沸闻鉴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽孟岛。三九已至,卻和暖如春督勺,著一層夾襖步出監(jiān)牢的瞬間渠羞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工智哀, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留次询,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓瓷叫,卻偏偏與公主長(zhǎng)得像屯吊,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子摹菠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容