This article mainly introduces the detailed tutorial of Vue2.0 parent-child component transfer function. Friends in need can refer to it. I hope it can help everyone.
What is Vue.js
Vue.js (pronounced /vjuː/, similar to view) is a progressive framework for building user interfaces. Unlike other heavyweight frameworks, Vue adopts a bottom-up incremental development design. Vue's core library only focuses on the view layer. It is not only easy to get started, but also easy to integrate with third-party libraries or existing projects. On the other hand, Vue is also fully capable of powering complex single-page applications when combined with single-file components and libraries supported by the Vue ecosystem.
Study notes: In vue2.0, when a parent component calls a child component, it wants to pass the function body in the parent component.
1. Through props: it needs to be passed from the child component Applicable when passing parameters to the parent component
// Parent component.vue
<template> <p> <ok-input :params='number' :callback='callbackNum'></ok-input> </p> </template> <script type="text/ecmascript-6"> import okInput from '../ok-input/okinput.vue'; export default { props: {}, data() { return { number: {}, callbackNum: function (x) { console.log(x); } }; }, methods: { }, components: { 'ok-input': okInput } }; </script>
// Child component.vue
<template> <p> <input v-model='numVal' @change='handleFun'></input> </p> </template> <script type="text/ecmascript-6"> import {Input, Select, Option, Button} from 'element-ui'; import 'element-ui/lib/theme-default/index.css'; export default { props: { params: { type: Object, default: { type: '' } }, callback: {} }, data() { return { x: 'hah', numVal: '' }; }, methods: { handleFun(val) { this.callback(val); // 将参数传回父组件中的回调函数 } }, components: { 'el-input': Input, } }; </script>
2. Through $emit: Just Applicable when obtaining the current operation object
// 父组件.vue <template> <p> <ok-input :params='inputs' @change='handleAge'></ok-input> </p> </template> <script type="text/ecmascript-6"> import okInput from '../ok-input/okinput.vue'; export default { props: {}, data() { return { number: {} }; }, methods: { handleAge(evt) { console.log(evt.target.value); // 接收从子组件传过来的当前对象 } }, components: { 'ok-input': okInput } }; </script>
// Child component.vue
<template> <p> <input v-model='numVal' @blur='handleChange'></input> </p> </template> <script type="text/ecmascript-6"> import {Input, Select, Option, Button} from 'element-ui'; import 'element-ui/lib/theme-default/index.css'; export default { props: { params: { type: Object, default: { type: '' } }, callback: {} }, data() { return { x: 'hah', numVal: '' }; }, methods: { handleChange(evt) { this.$emit('change', evt); // 将当前对象 evt 传递到父组件 }, }, components: { 'el-input': Input, } }; </script>
Related recommendations:
event bus non-parent-child component communication in vue Detailed explanation
react.js Parent-child component data binding real-time communication example display
Detailed explanation of abstract Vue public components
The above is the detailed content of Detailed explanation of Vue2.0 parent-child component transfer function. For more information, please follow other related articles on the PHP Chinese website!