Vue $root、$parent、$refs
Vue处理边界
parent、$refs
下面的功能都是有风险的,尽量避免使用
Vue 子组件可以通过 $root 属性访问父组件实例的属性和方法
<div id="app">
<root-obj></root-obj>
</div>
Vue.component('root-obj', {
data() {
return {
}
},
template: `<div>
<button @click='getRoot'>$Root</button>
</div>`,
methods: {
getRoot() {
console.log(this)
console.log(this.$root)
}
}
})
var app = new Vue({
el: '#app',
data: {
msg: 'Root'
}
})
parent 的区别
parent 都能够实现访问父组件的属性和方法,两者的区别在于,如果存在多级子组件,通过
root 访问得到的是根父组件
<div id="app">
<root-obj></root-obj>
</div>
Vue.component('root-obj', {
data() {
return {
}
},
template: `<div>
<button @click='getRoot'>子组件</button>
<child-component></child-component>
</div>`,
methods: {
getRoot() {
console.log(this)
console.log(this.$root)
console.log(this.$parent)
}
}
})
Vue.component('child-component', {
data() {
return {
}
},
template: `<div>
<button @click='getRoot'>子子组件</button>
</div>`,
methods: {
getRoot() {
console.log(this)
console.log(this.$root)
console.log(this.$parent)
}
}
})
var app = new Vue({
el: '#app',
data: {
msg: 'Root'
}
})
$refs 访问子组件实例
通过在子组件标签定义 ref 属性,在父组件中可以使用$refs 访问子组件实例
<button @click='refView'>通过ref访问子组件</button>
Vue.component('base-input', {
data() {
return {
msg: 'base-input'
}
},
template: `<input type='text'/>`
})
var app = new Vue({
el: '#app',
data: {
msg: 'Root'
},
methods: {
refView() {
console.log(this.$refs.baseInput)
this.$refs.baseInput.$el.focus()
}
}
})
Vue $root、$parent、$refs的更多相关文章
- Vue处理边界之$root、$parent、$refs
Vue处理边界之parent.$refs 下面的功能都是有风险的,尽量避免使用 1.Vue 子组件可以通过 $root 属性访问父组件实例的属性和方法 <div id="app&quo ...
- Knockout学习笔记之二($root,$parent及$data的区别)
以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下: 对于$root 与$parent的区别: $root refers to the view model appl ...
- Vue父子组件传值之——访问根组件$root、$parent、$children和$refs
Vue组件传值除了prop和$emit,我们还可以直接获取组件对象: 根组件: $root // 单一对象 表示当前组件树的根 Vue 实例,即new Vue({...根组件内容}).如果当前实例没有 ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-007-设置LDAP server比较密码(contextSource、root()、ldif()、)
一.LDAP server在哪 By default, Spring Security’s LDAP authentication assumes that the LDAP server is li ...
- Vue父子组件传值$parent , ref,$refs,props大总结
子组件: <template> <div class="child"> <slot name='meiyong'></slot> & ...
- 通信vue2.0组件
vue2.0组件通信各种情况总结与实例分析 Props在vue组件中各种角色总结 在Vue中组件是实现模块化开发的主要内容,而组件的通信更是vue数据驱动的灵魂,现就四种主要情况总结如下: 使用p ...
- yum安装指定版本软件包__20160308
举例子: 安装 libGL-devel 1. yum list libGL-devel 居然说没有匹配的包的信息... [root@CentOS6 ~]# yum list libGL-devel L ...
- 20160808_卸载OpenJDK
1.查看信息 [root@localhost ~]# rpm -qa | grep jdkjava-1.6.0-openjdk-devel-1.6.0.0-1.50.1.11.5.el6_3.x86_ ...
- vue 工作学习总结
配置ESlint yarn 初始化 yarn init yes 添加依赖 yarn add [package] 升级依赖 yarn upgrade [package] 移出依赖 yarn remove ...
随机推荐
- PowerDesigner_15连接Oracle11g,反向工程导出模型图
1.启动PowerDesigner2.菜单:File->ReverseEngineer->Database出来NewPhysicalDataModel对话框,DBMS选择ORACLEVer ...
- 【内部】Fiddler设置代理请求的方式
1.2 打开Fiiddler,设置如图步骤: 3.添加规则: 4.这里选择第三个选项: 5.选中^开始,空格结束的如图内容.复制你要代理的地址.如:http://wap.cmread.com/nap/ ...
- [IJCAI-17 口碑商家客流量预测]
IJCAI-17 口碑商家客流量预测 第 1 赛季截止日期 2017/03/14 赛制介绍 重要时间2月8日 08:00: 评测启动3月7日 10:00: 报 ...
- 【OpenCV开发】OpenCV:使用VideoCapture类进行视频读取和显示
原文来自:http://blog.csdn.net/thefutureisour/article/details/7530344 -------------以下为原文---------------这次 ...
- 【VS开发】【图像处理】基于灰度世界、完美反射、动态阈值等图像自动白平衡算法的原理、实现及效果
基于灰度世界.完美反射.动态阈值等图像自动白平衡算法的原理.实现及效果 白平衡是电视摄像领域一个非常重要的概念,通过它可以解决色彩还原和色调处理的一系列问题.白平衡是随着电子影像再现色彩真实 ...
- react 中 EventEmitter 事件总线机制
此机制可用于 react 中兄弟组件中的通信 npm install events -S 事件总线: // eventBus.js import {EventEmitter} from 'events ...
- (转)JMeter性能测试-服务器资源监控插件详解
零.引言 我们对被测应用进行性能测试时,除了关注吞吐量.响应时间等应用自身的表现外,对应用运行所涉及的服务器资源的使用情况,也是非常重要的方面,通过实时监控,可以准确的把握不同测试场景下服务器资源消耗 ...
- nodejs以对象做对象的key导致value一直被覆盖
问题描述 在开发中,实现技能状态的事件监听功能时,将状态对象作为key,存入事件管理器的监听列表,如下图: 实现后,运行程序,并没有报错,但是当某个事件发生时,只有一个状态被触发监听,而大多数状态 ...
- PostgreSQL创建只读账户
目前PostgreSQL并不能像MySQL一样直接对某个数据库赋予只读权限,现实中有研发需要新建一个用户然后赋予对某个数据库只读权限. 举例说明如何创建 用edbstore用户连接edbstore数据 ...
- PythonDay16
第十六章模块 今日内容 自定义模块 time模块 datetime模块 rendom模块 自定义模块 # import 模块 例:import time# 导入的是time模块中所有的内容(变量,函数 ...