Vue 组件
注册
Components 要确保在初始化根实例 之前 注册了组件
全局注册:
| 12
 3
 4
 5
 6
 7
 
 | Vue.component('my-component', {template: '<div>A custom component!</div>'
 })
 
 new Vue({
 el: '#example'
 })
 
 | 
局部注册:
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | var Child = {template: '<div>A custom component!</div>'
 }
 new Vue({
 el: '#example',
 components: {
 'my-component': Child
 }
 })
 
 | 
其他
is属性:
把自定义组件绑定在此元素上
| 12
 3
 
 | <table><tr is="my-row"></tr>
 </table>
 
 | 
data
在组件中必须是函数形式
| 12
 3
 4
 
 | data:function(){}
 
 data () {}
 
 | 
构成组件
props down, events up
Prop验证
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | Vue.component('example', {props: {
 
 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 value > 10
 }
 }
 }
 })
 
 | 
自定义事件
侦听子组件抛出的事件,必须用v-on在模板中绑定
绑定原生事件
子组件索引:ref
| 12
 3
 4
 5
 6
 7
 8
 
 | <div id="parent"><user-profile ref="profile"></user-profile>
 </div>
 <script>
 var parent = new Vue({ el: '#parent' })
 var child = parent.$refs.profile
 </script>
 和v-for一起用时,ref是个数组或对象
 
 | 
Slot
Slot 具名Slot 作用域插槽
动态组件 keep-alive
Vue 组件的 API 来自三部分: Props 允许外部环境传递数据给组件 Events 允许组件触发外部环境的副作用 Slots 允许外部环境将额外的内容组合在组件中