組件註冊 |
組件名 |
Vue.component('my-component-name', { /* ... */ })
第一個參數即組件名, 儘可能字母全小寫且必須包含一個連字符vue |
全局註冊 |
Vue.component('my-component-name', { /* ... */ })
|
局部註冊 |
import ComponentA from './ComponentA.vue'
export default {
components: {
ComponentA
},
// ...
}
|
模塊系統 |
局部註冊組件 |
import ComponentA from './ComponentA'
import ComponentC from './ComponentC'
export default {
components: {
ComponentA,
ComponentC
},
// ...
}
如此, ComponentA和ComponentC便可在B裏使用正則表達式 |
基礎組件的自動化全局註冊 |
全局註冊的行爲必須在 根 Vue 實例 (經過 new Vue) 建立以前發生數組
import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
const requireComponent = require.context(
// 其組件目錄的相對路徑
'./components',
// 是否查詢其子目錄
false,
// 匹配基礎組件文件名的正則表達式
/Base[A-Z]\w+\.(vue|js)$/
)
requireComponent.keys().forEach(fileName => {
// 獲取組件配置
const componentConfig = requireComponent(fileName)
// 獲取組件的 PascalCase 命名
const componentName = upperFirst(
camelCase(
// 獲取和目錄深度無關的文件名
fileName
.split('/')
.pop()
.replace(/\.\w+$/, '')
)
)
// 全局註冊組件
Vue.component(
componentName,
// 若是這個組件選項是經過 `export default` 導出的,
// 那麼就會優先使用 `.default`,
// 不然回退到使用模塊的根。
componentConfig.default || componentConfig
)
})
|
|
|
|
|
|
Prop |
prop的大小寫 |
HTML 中的特性名是大小寫不敏感的,因此瀏覽器會把全部大寫字符解釋爲小寫字符瀏覽器 在js裏camel, html模板裏kebeb, 如sampleText對應sample-text異步
Vue.component('blog-post', {
// 在 JavaScript 中是 camelCase 的
props: ['postTitle'],
template: '<h3>{{ postTitle }}</h3>'
})
<!-- 在 HTML 中是 kebab-case 的 -->
<blog-post post-title="hello!"></blog-post>
|
prop類型 |
除了只指定prop名字,async
props: ['title', 'likes', 'isPublished', 'commentIds', 'author']
還能夠指定其類型ide
props: {
title: String,
likes: Number,
isPublished: Boolean,
commentIds: Array,
author: Object,
callback: Function,
contactsPromise: Promise // or any other constructor
}
|
傳遞靜態或動態prop |
靜態 |
<blog-post title="My journey with Vue"></blog-post>
|
動態 |
任何類型的值均可以傳給一個 prop函數
<!-- 動態賦予一個變量的值 -->
<blog-post v-bind:title="post.title"></blog-post>
<!-- 動態賦予一個複雜表達式的值 -->
<blog-post
v-bind:title="post.title + ' by ' + post.author.name"
></blog-post>
|
|
單向數據流 |
全部的 prop 都使得其父子 prop 之間造成了一個單向下行綁定:父級 prop 的更新會向下流動到子組件中,post 可是反過來則不行。這樣會防止從子組件意外改變父級組件的狀態,從而致使你的應用的數據流向難以理解。 |
額外的,每次父級組件發生更新時,子組件中全部的 prop 都將會刷新爲最新的值。 這意味着你不該該在一個子組件內部改變 prop。若是你這樣作了,Vue 會在瀏覽器的控制檯中發出警告。 |
注意在 JavaScript 中對象和數組是經過引用傳入的, 因此對於一個數組或對象類型的 prop 來講,在子組件中改變這個對象或數組自己將會影響到父組件的狀態。 |
|
prop驗證 |
|
當 prop 驗證失敗的時候,(開發環境構建版本的) Vue 將會產生一個控制檯的警告。
Vue.component('my-component', {
props: {
// 基礎的類型檢查 (`null` 和 `undefined` 會經過任何類型驗證)
propA: Number,
// 多個可能的類型
propB: [String, Number],
// 必填的字符串
propC: {
type: String,
required: true
},
// 帶有默認值的數字
propD: {
type: Number,
default: 100
},
// 帶有默認值的對象
propE: {
type: Object,
// 對象或數組默認值必須從一個工廠函數獲取
default: function () {
return { message: 'hello' }
}
},
// 自定義驗證函數
propF: {
validator: function (value) {
// 這個值必須匹配下列字符串中的一個
return ['success', 'warning', 'danger'].indexOf(value) !== -1
}
}
}
})
那些 prop 會在一個組件實例建立以前進行驗證,因此實例的屬性 (如 data 、 computed 等) 在 default 或 validator 函數中是不可用的。 |
類型檢查 |
type:
null/undefined會經過一切類型檢查 |
|
非prop的特性 |
一個非 prop 特性是指傳向一個組件,可是該組件並無相應 prop 定義的特性。
|
組件能夠接受任意的特性,而這些特性會被添加到這個組件的根元素上。 |
替換/合併已有的特性 |
class, style |
外部傳入的和內部的合併 (內部的在前,傳入的在後) |
class, style之外 |
外部傳入的替換掉內部的 |
|
禁言特性繼承 |
若是你不但願組件的根元素繼承特性,你能夠在組件的選項中設置 inheritAttrs: false # TODO: 例子到底啥意思? |
|
自定義事件 |
事件名 |
始終使用 kebab-case 的事件名,全小寫。推薦update:prop名 例update:my-sample-prop |
自定義組件的v-model |
v-model默認利用叫value的prop和input事件 ● 改變默認, 用model選項 注意仍然須要在prop裏聲明
Vue.component('base-checkbox', {
model: {
prop: 'checked',
event: 'change'
},
props: {
checked: Boolean
},
template: `
<input
type="checkbox"
v-bind:checked="checked"
v-on:change="$emit('change', $event.target.checked)"
>
`
})
<base-checkbox v-model="lovingVue"></base-checkbox>
|
將原生事件綁定到組件 |
# TODO: finish here |
.sync修飾符 |
<text-document v-bind:title.sync="doc.title"></text-document>
至關於
<text-document
v-bind:title="doc.title"
v-on:update:title="doc.title = $event"
></text-document>
● 不能和表達式一塊兒用, 相似v-model, 只接屬性名 ● 同時添加多個
<text-document v-bind.sync="doc"></text-document>
這樣會把 doc 對象中的每個屬性 (如 title ) 都做爲一個獨立的 prop 傳進去,而後各自添加用於更新的 v-on 監聽器。 |
|
|
|
|
插槽 |
|
|
|
|
|
|
|
|
|
|
|
|
動態組件&異步組件 |
# TODO: finish here |
https://cn.vuejs.org/v2/guide/components-dynamic-async.html |
|
|
|
|
|
|
|
|
|
|
處理邊界狀況 |
# TODO: finish here |
在絕大多數狀況下,咱們最好不要觸達另外一個組件實例內部或手動操做 DOM 元素。不過也確實在一些狀況下作這些事情是合適的。 https://cn.vuejs.org/v2/guide/components-edge-cases.html |