search
HomeWeb Front-endJS TutorialHow to implement two-way binding between Vue2 parent component and child component

This time I will show you how to implement the two-way binding of the Vue2 parent component and the child component. What are the precautions ? The following is a practical case. Let’s take a look.

Recently I was studying how to write a set of UI components based on Vue2. To achieve two-way binding, I thought about using Vuex, but after observing other excellent UIframework, I found that using Vuex would cause trouble to other users, so I decided to find a solution and consulted several articles. After many articles, I finally found it.

I post the solution here, hoping to help colleagues like me who are new to the Vue framework.

Code logic of subcomponents

<template> 
 <p> 
  <input> 
 </p> 
</template> 
 
<style> 
 @import "../../assets/styles/form/form.less"; 
</style> 
<script> 
 export default { 
  name: &#39;NeIpt&#39;, 
  props: { 
   // 接收一个由父级组件传递过来的值 
   value: { 
    type: String, 
    default: function () { 
     return &#39;&#39; 
    } 
   } 
  }, 
  computed:{ 
   currentValue: { 
    // 动态计算currentValue的值 
    get:function() { 
     return this.value; // 将props中的value赋值给currentValue 
    }, 
    set:function(val) { 
     this.$emit(&#39;input&#39;, val); // 通过$emit触发父组件 
    } 
   } 
  } 
 } 
</script>

Parent component code logic

<template> 
 <p> 
  <ne-ipt></ne-ipt> 
 </p> 
</template> 
<style> 
</style> 
<script> 
 import NeIpt from &#39;./NeIpt&#39; 
 export default { 
  name: &#39;form-index&#39;, 
  data () { 
   return { 
    test: &#39;&#39; 
   } 
  }, 
  components: { 
   NeIpt 
  } 
 } 
</script>

When modifying the currentValue of the subcomponent, the input event is actually triggered through $emit and the value is passed to the caller's v-model, thereby achieving 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:

How to implement the file download function in Django

How to reference local static images in vue

The above is the detailed content of How to implement two-way binding between Vue2 parent component and child component. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
两个微信可以绑定同一张银行卡吗两个微信可以绑定同一张银行卡吗Aug 25, 2023 pm 03:13 PM

两个微信不可以绑定同一张银行卡。绑定银行卡到微信账户操作:1、打开微信应用程序,点击“我”选项,然后选择“支付”选项;2、选择“添加银行卡”选项,并按照系统提示输入银行卡信息;3、一旦银行卡绑定成功,用户就可以在微信中使用该银行卡进行支付和转账。

如何在Vue中实现可编辑的表格如何在Vue中实现可编辑的表格Nov 08, 2023 pm 12:51 PM

在许多Web应用程序中,表格是必不可少的一个组件。表格通常具有大量数据,因此表格需要一些特定的功能来提高用户体验。其中一个重要的功能是可编辑性。在本文中,我们将探讨如何使用Vue.js实现可编辑的表格,并提供具体的代码示例。步骤1:准备数据首先,我们需要为表格准备数据。我们可以使用JSON对象来存储表格的数据,并将其存储在Vue实例的data属性中。在本例中

vue2与vue3中的生命周期执行顺序有什么区别vue2与vue3中的生命周期执行顺序有什么区别May 16, 2023 pm 09:40 PM

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

BTCC教学:如何在BTCC交易所绑定使用MetaMask钱包?BTCC教学:如何在BTCC交易所绑定使用MetaMask钱包?Apr 26, 2024 am 09:40 AM

MetaMask(中文也叫小狐狸钱包)是一款免费的、广受好评的加密钱包软件。目前,BTCC已支持绑定MetaMask钱包,绑定后可使用MetaMask钱包进行快速登入,储值、买币等,且首次绑定还可获得20USDT体验金。在BTCCMetaMask钱包教学中,我们将详细介绍如何注册和使用MetaMask,以及如何在BTCC绑定并使用小狐狸钱包。MetaMask钱包是什么?MetaMask小狐狸钱包拥有超过3,000万用户,是当今最受欢迎的加密货币钱包之一。它可免费​​使用,可作为扩充功能安装在网络

小红书怎么绑定子账号?它怎么检测账号是否正常?小红书怎么绑定子账号?它怎么检测账号是否正常?Mar 21, 2024 pm 10:11 PM

在如今这个信息爆炸的时代,个人品牌和企业形象的建设变得越来越重要。小红书作为国内领先的时尚生活分享平台,吸引了大量用户关注和参与。对于那些希望扩大影响力、提高内容传播效率的用户来说,绑定子账号成为了一种有效的手段。那么,小红书怎么绑定子账号呢?又如何检测账号是否正常呢?本文将为您详细解答这些问题。一、小红书怎么绑定子账号?1.登录主账号:首先,您需要登录您的小红书主账号。2.打开设置菜单:点击右上角的“我”,然后选择“设置”。3.进入账号管理:在设置菜单中,找到“账号管理”或“账号助手”选项,点

快速搞懂Vue2 diff算法(图文详解)快速搞懂Vue2 diff算法(图文详解)Mar 17, 2023 pm 08:23 PM

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

今日头条中绑定抖音的步骤方法今日头条中绑定抖音的步骤方法Mar 22, 2024 pm 05:56 PM

1、打开今日头条。2、点击右下角我的。3、点击【系统设置】。4、点击【账号和隐私设置】。5、点击【抖音】右边的按钮即可绑定抖音。

菜鸟app怎么绑定拼多多 菜鸟裹裹怎么添加拼多多平台菜鸟app怎么绑定拼多多 菜鸟裹裹怎么添加拼多多平台Mar 19, 2024 pm 02:30 PM

  菜鸟app就是能够为你们提供出各种物流信息状况的平台,这里的功能非常的强大好用,大家有任何与物流相关的问题,都能在这得到解决的,反正都能为你们带来一站式的服务,全都能及时解决的,查快递,取快递,寄快递等,都是毫无任何问题,与各个的平台都进行了合作,全部的信息,都能查询得到的,但是有些时候会出现拼多多当中购买的商品,都是无法呈现出物流的信息,其实是需要大家进行手动绑定拼多多,才能实现的,具体的方法已经整理出来了在下方,大家都能来看看的。菜鸟绑定拼多多账户的方法:  1、打开菜鸟APP,在主页面

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools