[Vue] Parent and Child component communcation
By building components, you can extend basic HTML elements and reuse encapsulated code. Most options that are passed into a Vue constructor can be passed into a component. Each instance of a component has an isolated scope. Data can only be passed one direction and it must be from parent to child using props
. To communicate with the parent element, use the $emit
method.
Child component:
<template>
<section>
<h1>Child component {{name }}</h1>
<div>
Child button: <button @click="insideInc">Inc from inside {{insideCounter}}</button>
</div>
</section>
</template> <script>
export default {
name: 'item-description',
props: ['counter', 'name'],
data: function() {
return {
insideCounter: this.counter
}
},
methods: {
insideInc() {
this.insideCounter += 1;
this.$emit('total', this.insideCounter);
}
}
}
</script>
From parent, we will receive:
props: ['counter', 'name'],
And we rename 'counter' from parent to 'insideCounter':
data: function() {
return {
insideCounter: this.counter
}
},
If we want to tell parent component, we can use '$emit':
this.$emit('total', this.insideCounter);
From parent component:
<item-description
v-bind:counter = "counter"
v-bind:name = "message"
@total="getTotalFromChild"
></item-description>
<script> import ItemDescription from '../components/item-description';
components: {
ItemDescription
}, .. </script>
[Vue] Parent and Child component communcation的更多相关文章
- vue & child component & props
vue & child component & props vue pass data to child component https://vuejs.org/v2/guide/co ...
- [Angular 2] @ViewChild to access Child component's method
When you want to access child component's method, you can use @ViewChild in the parent: Parent Compo ...
- File(File f, String child) File(String parent, String child)
(转载)File(File f, String child) 根据f 抽象路径名和 child 路径名字符串创建一个新 File 实例. f抽象路径名用于表示目录,child 路径名字符串用于表示目录 ...
- e621. Activating a Keystroke When Any Child Component Has Focus
Normally, a keystroke registered on a component is activated when the component has the focus. This ...
- vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit
vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit
- Vue Parent Send Ajax Data to Child Component
Vue 父组件ajax异步更新数据,子组件props获取不到 2018年06月26日 09:25:06 哎哟嘿 阅读数 3585 当父组件 axjos 获取数据,子组件使用 props 接 ...
- Vue教程:组件Component详解(六)
一.什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功 ...
- [Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component
Components with slots can expose their data by passing it into the slot and exposing the data using ...
- Vue(八)---组件(Component)
组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码. 注册一个全局组件语法格式如下: Vue.component(tagName, optio ...
随机推荐
- Hbase技术详细学习笔记
注:转自 Hbase技术详细学习笔记 最近在逐步跟进Hbase的相关工作,由于之前对Hbase并不怎么了解,因此系统地学习了下Hbase,为了加深对Hbase的理解,对相关知识点做了笔记,并在组内进行 ...
- [D3] Add image to the node
We can create node with 'g' container, then append 'image' to the nodes. // Create container for the ...
- Python写爬虫-爬甘农大学校新闻
Python写网络爬虫(一) 关于Python: 学过C. 学过C++. 最后还是学Java来吃饭. 一直在Java的小世界里混迹. 有句话说: "Life is short, you ne ...
- git stash备份当前工作区内容,回到最近一次commit提交
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致.同时,将当前的工作区内容保存到Git栈中.git stash pop: 从Git栈中读取 ...
- HibernateCRUD基础框架(3)-简单的和较为复杂的标准的CRUD API
优点:简单的和基础的CRUD功能可以很快实现,可以说是比较的"标准化".维护起来也很容易. 缺点:性能没有保障.不支持特别复杂的CRUD. 可以适用的场景:小型Web项目 1.Cr ...
- java.util.ConcurrentModificationException(如何避免ConcurrentModificationException)
java.util.ConcurrentModificationException is a very common exception when working with java collecti ...
- 函数的引用透明性(referential transparency)
1. 基础 初学程序设计时,比较容易混淆的两个概念是数学函数(math function)和程序中使用的函数. 在数学函数中 y=f(x),一个输入值有固定的输出值.例如,无论计算多少次,sinπ 的 ...
- ios日期比较
+(int)compareDate:(NSDate *)date1 date:(NSDate *)date2 { NSDateFormatter *dateFormatter = [[NSDateFo ...
- C#实现自己主动升级(附源代码)
对于PC桌面应用程序而言,自己主动升级功能往往是不可缺少的. 而自己主动升级能够作为一个独立的C/S系统来开发,这样,就能够在不同的桌面应用中进行复用.本文将着重介绍OAUS的相关背景. ...
- tensorflow:图(Graph)的核心数据结构与通用函数(Utility function)
Tensorflow一些常用基本概念与函数(2) 1. 图(Graph)的核心数据结构 tf.Graph.__init__:建立一个空图: tf.Graph.as_default():一个将某图设置为 ...