vue.js 待學(xué)習(xí)

  • watcher
  • keyv-for
  • v-on:click.native="" 綁定原生事件?
  • 過濾器
  • 數(shù)據(jù)更新檢測
  • $event
  • 事件修飾符: .stop/.prevent/.capture/.self
  • 按鍵修飾符(別名)
  • v-model 具體原理
    v-bind動態(tài)綁定結(jié)合
    修飾符.lazy/.number/.trim

組件

  • 全局注冊
// 注冊
Vue.component('my-component', {
  template: '<div>A custom component!</div>'
})
// 創(chuàng)建根實例
new Vue({
  el: '#example'
})
  • 局部注冊
var Child = {
 template: '<div>A custom component!</div>'
}
new Vue({
 // ...
 components: {
   // <my-component> 將只在父模板可用
   'my-component': Child
 }
})
  • is屬性的使用
    <ul> ,<ol><table> ,<select>限制了能被它包裹的元素
<table>
  <tr is="my-row"></tr>
</table>
  • 組件中,data必須為函數(shù)
data: function () {
  return {
    counter: 0
  }
}
  • 組件間傳值
  1. props父傳子
    子組件需要顯式地用 props 選項 聲明 “prop”:
Vue.component('child', {
  // 聲明 props
  props: ['message'],
  // 就像 data 一樣,prop 可以用在模板內(nèi)
  // 同樣也可以在 vm 實例中像 “this.message” 這樣使用
  template: '<span>{{ message }}</span>'
})

傳入值為:

<div>
  <input v-model="parentMsg">
  <br>
  <child v-bind:my-message="parentMsg"></child>
</div>

如果 prop 是一個對象或數(shù)組,在子組件內(nèi)部改變它會影響父組件的狀態(tài)。

  • props驗證
    當(dāng) prop 驗證失敗了, Vue 將拒絕在子組件上設(shè)置此值,如果使用的是開發(fā)版本會拋出一條警告。
Vue.component('example', {
  props: {
    // 基礎(chǔ)類型檢測 (`null` 意思是任何類型都可以)
    propA: Number,
    // 多種類型
    propB: [String, Number],
    // 必傳且是字符串
    propC: {
      type: String,
      required: true
    },
    // 數(shù)字,有默認(rèn)值
    propD: {
      type: Number,
      default: 100
    },
    // 數(shù)組/對象的默認(rèn)值應(yīng)當(dāng)由一個工廠函數(shù)返回
    propE: {
      type: Object,
      default: function () {
        return { message: 'hello' }
      }
    },
    // 自定義驗證函數(shù)
    propF: {
      validator: function (value) {
        return value > 10
      }
    }
  }
})
  1. 自定義事件子傳父
  2. 非父子組建通信
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容