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

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

博客园 - 匡匡

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 的开发指引来处理组件之间的通信。