惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
SegmentFault 最新的问题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Attack and Defense Labs
Attack and Defense Labs
F
Full Disclosure
Vercel News
Vercel News
N
News | PayPal Newsroom
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
H
Heimdal Security Blog
P
Privacy International News Feed
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
L
Lohrmann on Cybersecurity
D
Docker
Recent Announcements
Recent Announcements
Security Archives - TechRepublic
Security Archives - TechRepublic
人人都是产品经理
人人都是产品经理
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
博客园 - 叶小钗
Google Online Security Blog
Google Online Security Blog
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
S
Secure Thoughts
博客园 - Franky
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Latest news
Latest news
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学

博客园 - 匡匡

Vue: 组件扩展 WebClient 指定出口 IP IIS8 下 JS, CSS 等静态文件出现 500 错误 使用 ffmpeg 转换 mov 视频 使用 ildasm 和 ilasm 修改程序集的的引用信息 2020-01-08 工作日记:无题 .Net Core 程序集管理说明(加载) .NET CORE 动态加载 DLL 的问题 ASP.NET 后台 COOKIE 的设置 使用 sql server 默认跟踪分析执行的 SQL 语句 Nginx深入详解之upstream分配方式 使用 HttpWebRequest 类做 POST 请求没有应反 webpack 里的 import, exports 实现原理 使用 pdf.js 查看发票时,显示不了台头和印章的解决办法 Flex 布局里 input 宽度最小 150px 的问题, 浏览器 BUG? 使用像素单位设置 EXCEL 列宽或行高 sweetalert 快速显示两个提示, 第二个显示不出的问题 加权轮询和加权随机算法 在 Docker 中部署 ASP.NET CORE 应用
Vue 父子组件通信方式
匡匡 · 2021-05-20 · via 博客园 - 匡匡

Vue 父子组件通信方式

以前在写 WinForm 程序, 或 WebForm 程序的时候,父子组件进行通信的时候一般是调用对方的属性或方法即可,

Vue 里面的组件有个 props 属性,用来设置父组件向子组件传递数据属性,子属性需要向父组件发起通知时,一般使用 this.$emit 方法引发一个事件,父组件需要监听此事来做处理。

<template>
    <div class="parent">
        <child :msg="msg" @change="change" />
    </div>
</template>
<script>
export default {
    data: function() {
        return {
            msg: "Hello"
        }
    },
    methods: {
        change: function(name) {
            this.msg = "Hello " + name;
        }
    }
}
</script>
<template>
    <div class="child">
        <span>{{msg}}</span>
        <br />
        <button @click="change">change</button>
    </div>
</template>
<script>
export default {
    props: {
        msg: String
    },
    methods: {
        change: function() {
            this.$emit('change', 'zhangshang');
        }
    }
}
</script>

在子组件里不能直接修改 props 属性的值,会出错,但是有时候我们只需要简单的修改一父组件的值,监听事件,发起事件感觉稍显麻烦,需要一个更直接方式来处理,

子组件调用父组件

在 Vue 的组件上有一个 $parent 的属性,指向组件的父组件,通过此属性可以直接调用父组件的方法:

this.$parent.methodName(arg);

// 直接设置属性
this.$parent.msg = "Hello World!!!"; // 直接设置父组件的

// 调用 $set 方法
this.$set(this.$parent, 'msg', 'Hello, World!!!');
this.$parent.$set(this.$parent, 'msg', 'Hello, World');
Vue.set(this.$parent, 'msg', 'Hello, World');

父组件调用子组件

和子组件调用父组件的方式一下,在父组件中只需要获取到了子组件的引用,通过此引用即可调用相关的方法或属性。

总结

通过引用来从外部修改组件内部的数据,这种方式只用于简单的场景,最好还是按 Vue 的开发指引来处理组件之间的通信。