创建: 2019/11/02
组件注册 | |||||||||||
组件名 |
Vue.component(‘my-component-name‘, { /* ... */ }) 第一个参数即组件名, 尽量字母全小写且必须包含一个连字符 |
||||||||||
全局注册 |
Vue.component(‘my-component-name‘, { /* ... */ })
|
||||||||||
局部注册 |
import ComponentA from ‘./ComponentA.vue‘ export default { components: { ComponentA }, // ... }
|
||||||||||
模块系统 |
|
||||||||||
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名字, props: [‘title‘, ‘likes‘, ‘isPublished‘, ‘commentIds‘, ‘author‘] 还可以指定其类型 props: { title: String, likes: Number, isPublished: Boolean, commentIds: Array, author: Object, callback: Function, contactsPromise: Promise // or any other constructor }
|
||||||||||
传递静态或动态prop |
|
||||||||||
单向数据流 |
|
||||||||||
prop验证 |
|
||||||||||
非prop的特性 |
一个非 prop 特性是指传向一个组件,但是该组件并没有相应 prop 定义的特性。
|
||||||||||
自定义事件 | |||||||||||
事件名 |
始终使用 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>
这样会把 |
||||||||||
插槽 | |||||||||||
动态组件&异步组件 | |||||||||||
# 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
|
||||||||||
原文:https://www.cnblogs.com/lancgg/p/11781508.html