访问元素&组件
使用
$root访问根实例;使用
parent访问父组件;使用
refs访问子组件:
// html<base-input ref="usernameInput"></base-input>// jsthis.$refs.usernameInput
- 使用依赖注入向子组件暴露方法或属性
// 父辈组件provide: function () {return {getMap: this.getMap}}// 子辈组件inject: ['getMap']
在程序中监听事件
有三个与事件有关的 API:
$on(eventName, value);$once(eventName, value);$off(eventName, value)。
组件的循环引用
递归调用自身
这种情况要确保有一个递归的 终点 。
循环依赖
循环依赖: A 中有 B, B 中有 A 。
使用
Vue.component注册,没有问题;使用 webpack 等工具,有问题。解决方法:
- 在
beforeCreate中添加组件
- 在
beforeCreate() {this.$options.components.B = require('./b.vue').default;}
ii. 使用异步加载
components: {TreeFolderContents: () => import('./tree-folder-contents.vue')}
强制更新
使用 this.$forceUpdate() 强制更新。
v-once 创建静态组件
如果某个组件创建之后不需要在更新,可以使用 v-once 指令,使其只渲染一次,之后便缓存起来。
<div v-once><h1>Terms of Service</h1>... a lot of static content ...</div>
