vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定。

不过它有一个前身,先来看看.sync出现之前是如何实现的

父组件中(传递给子组件一个值:propObj)

<template>
<div>
<Lala :propObj="lalala"
v-on:update:propObj="lalala = $event"
></Lala>
</div> </template> <script>
import Lala from '@/components/lala.vue'; export default {
components:{
Lala
},
data(){
return {
lalala:{name:"哈哈"},
}
}
}
</script>

子组件中(点击事件去更新父组件的值)

<template>
<div><el-button @click="dd()">自定义组件内的按钮</el-button>
</div>
</template> <script>
export default {
props:['propObj'],//外部传值
methods:{
dd(){
if(this.propObj.name==2){
this.propObj.name="哈哈";
this.$emit('update:propObj', this.propObj)
}else{
this.propObj.name=2;
this.$emit('update:propObj', this.propObj)
} }
}
}
</script>

下面用vue 修饰符sync来实现,会更简洁

父组件中

<template>
<div>
<Lala v-bind:propObj.sync="lalala"></Lala>
</div> </template> <script>
import Lala from '@/components/lala.vue'; export default {
components:{
Lala
},
data(){
return {
lalala:{name:"哈哈"},
}
}
}
</script>

子组件中保持不变

vue中.sync修饰符,实现子组件实时更新父组件的值的更多相关文章

  1. vue中.sync 修饰符

    一直以来,都不太明白.sync的用法,归根结底原因在于,没有仔细阅读“.sync修饰符”. 正好,最近在拿一个项目练手,然后使用了elment-ui,然后在用到dialog的时候,属性visible是 ...

  2. Vue中.sync修饰符

    Vue 中 sync的作用 <FatherComponent :a.sync = 'b'><FatherComponent /> 子组件中emit('update:a',... ...

  3. vue中的修饰符

    Vue2.0学习笔记:Vue事件修饰符的使用   事件处理 如果需要在内联语句处理器中访问原生DOM事件.可以使用特殊变量$event,把它传入到methods中的方法中. 在Vue中,事件修饰符处理 ...

  4. vue中通过.sync修饰符实现子组件修改父组件数据

    vue父子通讯是单向数据流,也就是子组件不能修改父组件的值,但是在一些情况下是需要这样做的. 先看官方文档: 接下来举例实现 1.实现一个双向数据绑定,子组件改变的时候,父组件也在改变 父组件 < ...

  5. vue 之 .sync 修饰符

    在一些情况下,我们可能会需要对一个 prop (父子组件传递数据的属性) 进行“双向绑定”. 在vue 1.x 中的 .sync 修饰符所提供的功能.当一个子组件改变了一个带 .sync 的prop的 ...

  6. vue的.sync 修饰符

    很多时候,我们会对 prop (父子组件传递数据的属性) 进行“双向绑定” 在vue 1.x 中的 .sync 修饰符所提供的功能.当一个子组件改变了一个带 .sync 的prop的值时,这个变化也会 ...

  7. 027——VUE中事件修饰符:stop prevent self capture

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 026——VUE中事件修饰符之使用$event与$prevent修饰符操作表单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. VUE中事件修饰符:stop prevent self capture

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

随机推荐

  1. Qt里的原子操作QAtomicInteger,有挑战性,使用Q_ATOMIC_INT{nn}_IS_SUPPORTED测试系统是否支持

    所谓原子操作,即一系列复杂的操作能一气呵成,中间不被其他的操作打断.这在多线程程序中尤其常见,但要实现这种功能,既要考虑程序的良好设计,又要关心特定平台的体系结构和相关编译器对原子特性的支持程度.所以 ...

  2. 回归损失函数:L1,L2,Huber,Log-Cosh,Quantile Loss

    回归损失函数:L1,L2,Huber,Log-Cosh,Quantile Loss 2019-06-04 20:09:34 clover_my 阅读数 430更多 分类专栏: 阅读笔记   版权声明: ...

  3. sql注入测试(2)---实例测试

    以下篇幅,用一个简单的实例说明如何进行测试. 功能:根据用户NAME删除用户,采用的是SQL拼接的方式,核心代码部分如下: public static void deleteByName(String ...

  4. windows环境下 快速杀死占用端口的进程

    保存为bat脚本,设置需要解除占用的端口 port,点击运行即可 @echo off setlocal enabledelayedexpansion set prot = 8022 for /f &q ...

  5. kong命令(四)upstream

    介绍 upstream 就是一个虚拟的服务.可用于配置多个target目标服务时实现负载均衡的效果. 注意:service的host指的就是upstream的name. 同时upstream提供了一个 ...

  6. Ubuntu的apt-get代理设置

    三种方法 -o选项 # sudo apt-get -o Acquire::http::proxy="http://127.0.0.1:8080/" update 配置文件 # vi ...

  7. extern c 解释

    extern "C"修饰的变量和函数是按照c的方式编译的 如果想用c++方式编译c代码,需要特殊标识 方法 #if defined(__cplusplus) || defined( ...

  8. TCP三次握手过程中涉及的队列知识的学习

    先上一张图 (图片来源:http://www.cnxct.com/something-about-phpfpm-s-backlog/) 如上图所示,这里有两个队列:syns queue(半连接队列): ...

  9. [Selenium3+python3.6]自动化测试2-入门

    参考http://www.cnblogs.com/yoyoketang/p/6123890.html #coding=utf-8 #Import webdriver time module from ...

  10. Python asyncio 模块

    Python 3.4 asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLo ...