个人小总结:1年多没有写博客,感觉很多知识点生疏了,虽然工作上能解决问题,但是当别人问到某个知识点的时候,还是迷迷糊糊的,所以坚持写博客是硬道理的,因为大脑不可能把所有的知识点记住,有可能某一天忘了,但是我们工作上还是会使用,只是理论忘了,所以写博客的好处是可以把之前的东西重新看一遍后会在大脑里面重新浮现起来,特别在面试的时候,别人问你的知识点的时候答不上来那种尴尬,但是平时经常使用到,只是说不出所以来的,因此写博客是最好的思路。

阅读目录

Vue2--父子组件的访问

父组件访问子组件,子组件访问父组件,或者子组件访问根组件,Vue.js 都提供了相对应的API。
1. 父组件访问子组件,使用 $children 或 $refs
2. 子组件访问父组件;使用 $parent
如下代码定义了 父组件<parent-component>,父组件模板定义了2个子组件;在父组件中,通过 this.$children 可以访问子组件。
this.$children 是一个数组,它包含所有子组件的实列;如下代码:

<!DOCTYPE html>
<html>
<body>
<head>
<title>演示Vue</title>
</head>
<div id='app'>
<parent-component></parent-component>
</div>
<template id="child-component1">
<p>{{ msg }}</p>
</template>
<template id="child-component2">
<p>{{ msg }}</p>
</template>
</body>
<script src="./vue.js"></script>
<script type="text/javascript">
Vue.component('parent-component', {
template: '<div><child-component1></child-component1><child-component2></child-component2><button @click="showmsg">显示子组件的数据</button></div>',
components: {
'child-component1': {
template: '#child-component1',
data: function() {
return {
msg: '这是子组件1'
}
}
},
'child-component2': {
template: '#child-component2',
data: function() {
return {
msg: '这是子组件2'
}
}
}
},
methods: {
showmsg: function() {
for (var i = 0; i < this.$children.length; i++) {
alert(this.$children[i].msg);
}
}
}
});
new Vue({
el: '#app'
})
</script>
</html>

查看效果

理解$refs
在子组件上使用 ref指令,可以给子组件指定一个索引ID,如下代码:

<child-component1 ref="A1"></child-component1><child-component2 ref="A2"></child-component2>

在父组件中,则通过$refs.索引ID访问子组件的实例:

showmsg: function() {
alert(this.$refs.A1.msg);
alert(this.$refs.A2.msg);
}

所有的代码如下:

<!DOCTYPE html>
<html>
<body>
<head>
<title>演示Vue</title>
</head>
<div id='app'>
<parent-component></parent-component>
</div>
<template id="child-component1">
<p>{{ msg }}</p>
</template>
<template id="child-component2">
<p>{{ msg }}</p>
</template>
</body>
<script src="./vue.js"></script>
<script type="text/javascript">
Vue.component('parent-component', {
template: '<div><child-component1 ref="A1"></child-component1><child-component2 ref="A2"></child-component2><button @click="showmsg">显示子组件的数据</button></div>',
components: {
'child-component1': {
template: '#child-component1',
data: function() {
return {
msg: '这是子组件1'
}
}
},
'child-component2': {
template: '#child-component2',
data: function() {
return {
msg: '这是子组件2'
}
}
}
},
methods: {
showmsg: function() {
alert(this.$refs.A1.msg);
alert(this.$refs.A2.msg);
}
}
});
new Vue({
el: '#app'
})
</script>
</html>

查看效果

理解$parent
下面有一个子组件 child-component 和一个父组件 parent-component, 在子组件中,通过 this.$parent 可以访问到父组件的实例;如下代码:

<!DOCTYPE html>
<html>
<body>
<head>
<title>演示Vue</title>
</head>
<div id='app'>
<parent-component></parent-component>
</div>
<template id="child-component">
<div>
<p>111111</p>
<button @click="showmsg">显示父组件的实例</button>
</div>
</template>
</body>
<script src="./vue.js"></script>
<script type="text/javascript">
Vue.component('parent-component', {
template: '<div><child-component></child-component></div>',
components: {
'child-component': {
template: '#child-component',
methods: {
showmsg: function() {
alert(this.$parent.msg);
}
}
}
},
data: function() {
return {
msg: 'parent component msg'
}
}
});
new Vue({
el: '#app'
})
</script>
</html>

查看效果

Vue2---父子组件之间的访问的更多相关文章

  1. Vue2.0父子组件之间和兄弟组件之间的数据交互

    熟悉了Vue.js的同级组件之间通信,写此文章,以便记录. Vue是一个轻量级的渐进式框架,对于它的一些特性和优点,请在官网上进行查看,不再赘述. 使用NPM及相关命令行工具初始化的Vue工程,目录结 ...

  2. Vue2.0父子组件之间的双向数据绑定问题解决方案

    对于vue 1.0项目代码,如果把vue换成vue 2.0,那么之后项目代码就完全奔溃不能运行,vue 2.0在父子组件数据绑定的变化(不再支持双向绑定)颠覆了1.0的约定,很遗憾. 解决方案只有两种 ...

  3. Vue.js 父子组件之间通信的方式

    Vue 父子组件之间的同学有一下几种方式: 1. props 2. $emit -- 组件封装用的比较多 3. .sync -- 语法糖 4. $attrs 和 $listeners -- 组件封装用 ...

  4. Vue(基础四)_总结五种父子组件之间的通信方式

    一.前言 这篇文章主要总结了几种通信方式: 1.方式一:使用props: [ ]和$emit()  (适用于单层通信) 2.方式二:$attrs和$listeners(适用于多层) 3.方式三:中央处 ...

  5. 【Vue课堂】Vue.js 父子组件之间通信的十种方式

    这篇文章介绍了Vue.js 父子组件之间通信的十种方式,不管是初学者还是已经在用 Vue 的开发者都会有所收获.无可否认,现在无论大厂还是小厂都已经用上了 Vue.js 框架,简单易上手不说,教程详尽 ...

  6. vue之父子组件之间的通信方式

    (一)props与$emit <!-这部分是一个关于父子组件之间参数传递的例子--> <!--父组件传递参数到子组件是props,子组件传递参数到父组件是用事件触发$emit--&g ...

  7. 【转】vue父子组件之间的通信

    vue父子组件之间的通信 在vue组件通信中其中最常见通信方式就是父子组件之中的通性,而父子组件的设定方式在不同情况下又各有不同.最常见的就是父组件为控制组件子组件为视图组件.父组件传递数据给子组件使 ...

  8. React 学习(六) ---- 父子组件之间的通信

    当有多个组件需要共享状态的时候,这就需要把状态放到这些组件共有的父组件中,相应地,这些组件就变成了子组件,从而涉及到父子组件之间的通信.父组件通过props 给子组件传递数据,子组件则是通过调用父组件 ...

  9. vue中父子组件之间的传值、非父子组件之间的传值

    在Vue实例中每个组件之间都是相互独立的,都有自己的作用域,所以组件之间是不能直接获取数据.在项目开发中一个组件可能需要获取另一个组件的值,我们可以通过其他方法间接的获取.所以,获取的方法有以下几种: ...

  10. vuejs组件交互 - 01 - 父子组件之间的数据交互

    父子组件之间的数据交互遵循: props down - 子组件通过props接受父组件的数据 events up - 父组件监听子组件$emit的事件来操作数据 示例 子组件的点击事件函数中$emit ...

随机推荐

  1. Spring全家桶系列–SpringBoot之AOP详解

    //本文作者:cuifuan //本文将收录到菜单栏:<Spring全家桶>专栏中 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关 ...

  2. prototype、proto和constructor的三角关系

    转载整理自http://www.cnblogs.com/xiaohuochai/p/5721552.html#3760057 http://blog.csdn.net/jasonzds/article ...

  3. Python全栈学习_day009知识点

    今日大纲: . 函数的初识 . 函数的返回值 . 函数的参数 1. 函数的初识 统计字符串s的总个数(不能用len) s='fkahfkahofijalkfkadhfkjadhf' count = f ...

  4. sql server: 最短路径

    --------------------------------------------------------------------- -- Road System 道路 ------------ ...

  5. cors解决跨域问题

    在作前后端分离的时候,我们总是要做跨域处理. 使用 express 框架搭建项目的时候可以设置如下: app.use(function (req, res, next) { res.setHeader ...

  6. MySQL 性能优化--优化数据库结构之优化数据大小

    MySQL性能优化--优化数据库结构之优化数据大小   By:授客  QQ:1033553122 尽量减少表占用的磁盘空间.通常,执行查询期间处理表数据时,小表占用更少的内存. 表列 l   尽可能使 ...

  7. 自定义控件详解(二):Path类 相关用法

    Path:路径 绘制路径:void drawPath (Path path, Paint paint) Path 可以绘制的路径 一.直线路径 1.基本方法 void moveTo (float st ...

  8. LeanCloud云引擎相关问题

    (1).Windows 用户可以在 Github releases 页面 根据操作系统版本下载最新的 32 位 或 64 位 msi 安装包进行安装,安装成功之后在 Windows 命令提示符(或 P ...

  9. vue axios 发送post请求,后端接收参数为null

    1首先检查自己的传参方式是否正确,我是传一个对象,没有问题,接口也触发了 2查了下资料说是 Content-Type的问题,设置为   'application/x-www-form-urlencod ...

  10. python与MongoDB的基本交互:pymongo

    本文内容: pymongo的使用: 安装模块 导入模块 连接mongod 获取\切换数据库 选择集合 CRUD操作 首发时间:2018-03-18 20:11 pymongo的使用: 安装模块: pi ...