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 ...
随机推荐
- Oracle 无备份情况下的恢复--临时文件/在线重做日志/ORA-00205
13.5 恢复临时文件 临时文件没有也不应该备份.通过V$TEMPFILE可以找到所有的临时文件. 此类文件的损坏会造成需要使用临时表空间的命令执行失败,不至于造成实例崩溃或session中断.由于临 ...
- 做筛选遍历时遇到的json字符串、json对象、json数组 三者之间的转换问题
这个是后台对登录用户以及筛选条件的操作 @Override public List<SdSdPer> listResults(String sidx, String sord) { try ...
- Service Mesh体验
前言# 计算机软件技术发展到现在,软件架构的演进无不朝着让开发者能够更加轻松快捷地构建大型复杂应用的方向发展.容器技术最初是为了解决运行环境的不一致问题而产生的,随着不断地发展,围绕容器技术衍生出来越 ...
- 在Opencv中将一幅图像均分成M* N个小图像
std::vector<std::vector<Mat> > partitionImage(Mat&src,int rows,int cols) 函数中有三个输入参数, ...
- LeetCode.977-排序数组的平方(Squares of a Sorted Array)
这是悦乐书的第369次更新,第397篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第231题(顺位题号是977).给定一个整数数组A按有序递增顺序排序,返回每个数字的平方, ...
- vue父子组件相互传值的实例
当子组件需要向父组件传递数据时,就要用到自定义事件 子组件用 $emit()来触发事件,父组件用$on()来监昕子组件的事件 父组件也可以直接在子组件的自定义标签上使用 v-on 来监昕子组件触发的自 ...
- htc 手机
是否解锁locked unlocked 然后刷入REC
- 第五周总结&实验·
本周总结 1.final声明的变量即成为常量,常量不可以修改. 2.子类能够 ...
- 搭建属于自己的图床(基于阿里云OSS,成本9元+20分钟)
之前的笔记都存在有道云笔记上,慢慢转化为Markdown格式分享出来,这中间遇到了一个图片问题,找了好久,了解到图床,然后找了谷歌插件中微博图床插件,用了一段时间发现,每次都需要登录微博,然后我又是一 ...
- Qt - 基于TCP的网络编程
TCP(传输控制协议 Transmission Control Protocol) 可靠.面向数据流.面向连接 的传输协议.(许多应用层协议都是以它为基础:HTTP.FTP) 使用情况: 相比UDP ...