This time I will bring you the two-way binding between the parent component and the child component in Vue2. What are the two-way binding between the parent component and the child component in Vue2? What are the precautions?, the following are Let’s take a look at practical cases.
Recently I am studying how to write a set of UI components based on Vue2. (parent component) data achieves two-way binding. I thought about using Vuex, but after observing other excellent UI frameworks, I found that using Vuex would cause trouble to other users, so I decided to find a solution. After referring to several articles After many articles, I finally found it.
I post the solution here, hoping to help colleagues like me who are coming into contact with the Vue framework for the first time.
The code logic of the child component
<template> <p> <input> </p> </template> <style> @import "../../assets/styles/form/form.less"; </style> <script> export default { name: 'NeIpt', props: { // 接收一个由父级组件传递过来的值 value: { type: String, default: function () { return '' } } }, computed:{ currentValue: { // 动态计算currentValue的值 get:function() { return this.value; // 将props中的value赋值给currentValue }, set:function(val) { this.$emit('input', val); // 通过$emit触发父组件 } } } } </script>
The code logic of the parent component
<template> <p> <ne-ipt></ne-ipt> </p> </template> <style> </style> <script> import NeIpt from './NeIpt' export default { name: 'form-index', data () { return { test: '' } }, components: { NeIpt } } </script>
When modifying the currentValue of the child component, actually Trigger the input event through $emit and pass the value to the caller's v-model to achieve two-way binding.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The use of v-for index index in html
vue packaged font and image resources How to deal with failure
The above is the detailed content of Two-way binding between parent component and child component in Vue2. For more information, please follow other related articles on the PHP Chinese website!

vue2与vue3中生命周期执行顺序区别生命周期比较vue2中执行顺序beforeCreate=>created=>beforeMount=>mounted=>beforeUpdate=>updated=>beforeDestroy=>destroyedvue3中执行顺序setup=>onBeforeMount=>onMounted=>onBeforeUpdate=>onUpdated=>onBeforeUnmount=&g

Vue是目前最流行的前端框架之一,而VUE3则是Vue框架的最新版本,相较于VUE2,VUE3具备了更高的性能和更出色的开发体验,成为了众多开发者的首选。在VUE3中,使用extends继承组件是一个非常实用的开发方式,本文将为大家介绍如何使用extends继承组件。extends是什么?在Vue中,extends是一个非常实用的属性,它可以用于子组件继承父

diff算法是一种通过同层的树节点进行比较的高效算法,避免了对树进行逐层搜索遍历。那么大家对diff算法吗有多少了解?下面本篇文章就来带大家深入解析下vue2的diff算法,希望对大家有所帮助!

Vue怎么通过JSX动态渲染组件?下面本篇文章给大家介绍一下Vue高效通过JSX动态渲染组件的方法,希望对大家有所帮助!

在VSCode中开发Vue/React组件时,怎么实时预览组件?本篇文章就给大家分享一个VSCode 中实时预览Vue/React组件的插件,希望对大家有所帮助!

在现代Web开发中,组件化是一个极受欢迎的开发模式。而Vue.js则是一个非常适合组件化的前端框架。在这篇文章中,我们将介绍如何使用Vue.js创建一个仿照片墙组件。在开始之前,我们需要明确一些准备工作。首先,我们需要安装Vue.js。安装的方法非常简单,只需在终端中输入以下命令:npminstallvue安装完成后,我们可以创建一个名为

VUE3开发入门教程:使用组件实现分页分页是一个常见的需求,因为在实际开发中,我们往往需要将大量的数据分成若干页以展示给用户。在VUE3开发中,可以通过使用组件实现分页功能,本文将介绍如何使用组件实现简单的分页功能。1.创建组件首先,我们需要创建一个分页组件,使用“vuecreate”命令创建VUE项目,并在src/components目录下创建Pagin

Vue是一款优秀的前端框架,在处理大量数据时,分页组件是必不可少的。分页组件可以使页面更加整洁,同时也可以提高用户体验。在Vue中,实现一个分页组件并不复杂,本文将介绍Vue如何实现分页组件。一、需求分析在实现分页组件前,我们需要对需求进行分析。一个基本的分页组件需要具有以下功能:展示当前页数、总页数以及每页展示条数点击分页按钮可以切换至不同页数展示当前页


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool