Vue处理边界之$root、$parent、$refs
Vue处理边界之
parent、$refs
下面的功能都是有风险的,尽量避免使用
1.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'
}
})
2.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'
}
})
3.$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()
}
}
})
原文:https://www.jianshu.com/p/2993c2cd6d68
Vue处理边界之$root、$parent、$refs的更多相关文章
- vue组件的那些事($children,$refs,$parent)的使用
如果项目很大,组件很多,怎么样才能准确的.快速的寻找到我们想要的组件了?? 1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"&g ...
- vue组件---边界处理情况
(1)访问元素&组件 ①访问根实例 在每个 new Vue 实例的子组件中,其根实例可以通过 $root 属性进行访问.例如,在这个根实例中: // Vue 根实例 new Vue({ dat ...
- 学习笔记:Vue——处理边界情况
访问元素&组件 01.访问根实例 $root // Vue 根实例 new Vue({ data: { foo: 1 }, computed: { bar: function () { /* ...
- Vue父子组件传值$parent , ref,$refs,props大总结
子组件: <template> <div class="child"> <slot name='meiyong'></slot> & ...
- Vue $root、$parent、$refs
Vue处理边界parent.$refs 下面的功能都是有风险的,尽量避免使用 Vue 子组件可以通过 $root 属性访问父组件实例的属性和方法 <div id="app"& ...
- Vue.js中ref ($refs)用法举例总结
原文地址:http://www.cnblogs.com/xueweijie/p/6907676.html <div id="app"> <input type=& ...
- vue问题整理
生命周期面试题 1.什么是 vue 生命周期 vue 实例从创建到销毁的过程就是生命周期. 也就是从开始创建.初始化数据.编译模板.挂在 dom -> 渲染.更新 -> 渲染.卸载等一系列 ...
- Vue入门学习
目录 Vue 简介 第一个Vue程序 Vue基本语法 双向绑定 组件 Axios异步通信 计算属性 Slot 自定义事件 第一个Vue-cli程序 webpack学习使用 Vue-Router路由 v ...
- Vue.js——60分钟组件快速入门(下篇)
概述 上一篇我们重点介绍了组件的创建.注册和使用,熟练这几个步骤将有助于深入组件的开发.另外,在子组件中定义props,可以让父组件的数据传递下来,这就好比子组件告诉父组件:"嘿,老哥,我开 ...
随机推荐
- nginx installl
参考http://jingyan.baidu.com/album/4b07be3cbbb54848b380f322.html?picindex=5 安装nginx需要的依赖包 wget 下载 编译安装 ...
- 1047: [HAOI2007]理想的正方形
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4075 Solved: 2277[Submit][Status][Discuss] Descript ...
- C++ 学习笔记 (六) 继承- 子类与父类有同名函数,变量
学习了类的继承,今天说一下当父类与子类中有同名函数和变量时那么程序将怎么执行.首先明确当基类和子类有同名函数或者变量时,子类依然从父类继承. 举例说明: 例程说明: 父类和子类有同名的成员 data: ...
- C++性能优化笔记
最近着手去优化项目中一个模块的性能.该模块是用C++实现,对大量文本数据进行处理. 一开始时,没什么思路,因为不知道性能瓶颈在哪里.于是借助perf工具来对程序进行分析,找出程序的性能都消耗在哪里了. ...
- cmf5分页相关
//分页配置在app/config.php 'paginate' => [ 'type' => '\cmf\paginator\Bootstrap', 'var_page' => ' ...
- python3 练习题100例 (八)
题目八:暂停一秒输出,并格式化当前时间. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目八:暂停一秒输出,并格 ...
- HDU - 6514 Monitor(二维差分)
题意 给定一个\(n×m\)的矩阵.(\(n×m <= 1e7\)). \(p\)次操作,每次可以在这个矩阵中覆盖一个矩形. \(q\)次询问,每次问一个矩形区域中,是否所有的点都被覆盖. 解析 ...
- 最小生成树:POJ1251-Jungle Roads(最小生成树的模板)
POJ 1251 Jungle Roads >[poj原址:http://poj.org/problem?id=1251](http://poj.org/problem?id=1251) Des ...
- 2018年江西理工大学C语言程序设计竞赛(高级组) 三角平方数
题目描述 三角数:形如图a,圆点摆放成等边三角形的数字,则为三角数. (图a) 平方数:形如图b,小方块摆放成正方形的数字,则为平方数. (图b) 那么如果一个数字既是三角形数又是平方数,则称为三角平 ...
- linux centos6 系统优化脚本-经典
转载一篇Ricky的系统优化脚本,这个脚本只能针对centos6x 其他还没有测试,但centos7肯定不行的 #!/bin/bash # ID 201510192126 # Author Ricky ...