合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 父子组件之间的通信 # 一般开发流程 - 比如在一个页面中 我们从服务器请求到了很多的数据 - 其中一部分数据 并非使我们整个页面的大组件来展示的 而是需要下面的子组件进行展示 - 这个时候 并不会让子组件再次发送一个网络强求 而是直接让大组件(父组件将数据传递给小组件(子组件) - 因为 向服务器发送网路请求 对服务器压力是很大的 # 如何进行父子组件间的通信呢? 官方解释: - 通过props向子组件传递数据 props是properties(属性)单词的缩写 - 通过事件向父组件发送消息 (emit) ![](https://img.kancloud.cn/6f/d9/6fd97968594a31f253877017476ec33e_459x202.png) 父组件通过----->props----->子组件 子组件---->$emit 自定义事件--->传递给父组件 ``` <pre class="calibre13">``` 父组件id是 app 子组件id是cpn 在父组件中传值给子组件 <span class="token"><</span>cpn v<span class="token">-</span>bind<span class="token1">:</span>cmovies <span class="token">=</span> <span class="token4">"movies"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>cpn<span class="token">></span> 子组件定义属性props<span class="token1">:</span><span class="token1">[</span><span class="token4">'cmovies'</span><span class="token1">]</span> cmovies里面就有了数据 第一种方式: 父组件定义数据movies 子组件进行绑定 即可传值到子组件 第二种方式: 在子组件中定义props属性:props<span class="token1">:</span><span class="token1">[</span><span class="token4">'cmessage'</span><span class="token1">]</span> 在父组件中定义数据 message 在子组件模板中 <span class="token3">增加data</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> <span class="token2">return</span> <span class="token1">{</span><span class="token1">}</span><span class="token1">}</span> 子模板 中使用 <span class="token1">{</span><span class="token1">{</span>cmessage<span class="token1">}</span><span class="token1">}</span>也可以拿到父组建的值 ``` ``` # 父传子 ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html lang<span class="token">=</span><span class="token4">"en"</span><span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"UTF-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Title<span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 传递数据<span class="token">--</span><span class="token">></span> <span class="token"><</span>cpn <span class="token1">:</span>cmovies<span class="token">=</span><span class="token4">"movies"</span> <span class="token1">:</span>cmessage<span class="token">=</span><span class="token4">"message"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>cpn<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>template id<span class="token">=</span><span class="token4">"cpn"</span><span class="token">></span> <span class="token"><</span>div<span class="token">></span> <span class="token1">{</span><span class="token1">{</span>cmovies<span class="token1">}</span><span class="token1">}</span> <span class="token1">{</span><span class="token1">{</span>cmessage<span class="token1">}</span><span class="token1">}</span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>template<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.jsdelivr.net/npm/vue/dist/vue.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 父组件向子组件传递数据<span class="token">--</span><span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token2">const</span> cpn <span class="token">=</span> <span class="token1">{</span> <span class="token6">//创建子组件</span> template<span class="token1">:</span> <span class="token4">'#cpn'</span><span class="token1">,</span> <span class="token6">//props:['cmovies','cmessage'], //接收父组件传值 数组形式</span> <span class="token6">//下面演示对象传递的形式</span> props<span class="token1">:</span><span class="token1">{</span> cmessage<span class="token1">:</span> <span class="token1">{</span> type<span class="token1">:</span> String<span class="token1">,</span> <span class="token2">default</span><span class="token1">:</span> <span class="token4">'aaaaa'</span> <span class="token1">}</span><span class="token1">,</span> cmovies<span class="token1">:</span> <span class="token1">{</span> type<span class="token1">:</span> Array<span class="token1">,</span> <span class="token2">default</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token1">[</span><span class="token1">]</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token2">const</span> app <span class="token">=</span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> <span class="token6">//父组件</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> message<span class="token1">:</span> <span class="token4">"测试数据"</span><span class="token1">,</span> movies<span class="token1">:</span> <span class="token1">[</span><span class="token4">'哪吒'</span><span class="token1">,</span><span class="token4">'海贼王'</span><span class="token1">,</span><span class="token4">'斗破苍穹'</span><span class="token1">,</span><span class="token4">'斗罗大陆'</span><span class="token1">]</span> <span class="token1">}</span><span class="token1">,</span> components<span class="token1">:</span> <span class="token1">{</span> <span class="token6">//注册组件</span> cpn <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ``` # 子-->父通信 ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html lang<span class="token">=</span><span class="token4">"en"</span><span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"UTF-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Title<span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span>父组件模板<span class="token">--</span><span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 对事件进行监听<span class="token">--</span><span class="token">></span> <span class="token"><</span>cpn @itemclick<span class="token">=</span><span class="token4">"cpnClick"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>cpn<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span>子组件模板<span class="token">--</span><span class="token">></span> <span class="token"><</span>template id<span class="token">=</span><span class="token4">"cpn"</span><span class="token">></span> <span class="token"><</span>div<span class="token">></span> <span class="token"><</span>button v<span class="token">-</span><span class="token2">for</span><span class="token">=</span><span class="token4">"item in infos"</span> @click<span class="token">=</span><span class="token4">"btnClick(item)"</span><span class="token">></span><span class="token1">{</span><span class="token1">{</span>item<span class="token1">.</span>name<span class="token1">}</span><span class="token1">}</span><span class="token"><</span><span class="token">/</span>button<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>template<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.jsdelivr.net/npm/vue/dist/vue.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token6">// 1 子组件</span> <span class="token2">const</span> cpn <span class="token">=</span> <span class="token1">{</span> template<span class="token1">:</span> <span class="token4">'#cpn'</span><span class="token1">,</span> <span class="token3">data</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> <span class="token2">return</span><span class="token1">{</span> infos<span class="token1">:</span> <span class="token1">[</span> <span class="token1">{</span>id<span class="token1">:</span> <span class="token4">'1'</span><span class="token1">,</span>name<span class="token1">:</span> <span class="token4">'热门推荐'</span> <span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span>id<span class="token1">:</span> <span class="token4">'2'</span><span class="token1">,</span>name<span class="token1">:</span> <span class="token4">'手机数码'</span> <span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span>id<span class="token1">:</span> <span class="token4">'3'</span><span class="token1">,</span>name<span class="token1">:</span> <span class="token4">'家用家电'</span> <span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span>id<span class="token1">:</span> <span class="token4">'4'</span><span class="token1">,</span>name<span class="token1">:</span> <span class="token4">'电脑办公'</span> <span class="token1">}</span> <span class="token1">]</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">,</span> methods<span class="token1">:</span> <span class="token1">{</span> <span class="token3">btnClick</span><span class="token1">(</span>item<span class="token1">)</span> <span class="token1">{</span> <span class="token6">//console.log(item)</span> <span class="token6">//itemClick 发射事件的名称 item 是发送的参数</span> <span class="token2">this</span><span class="token1">.</span><span class="token3">$emit</span><span class="token1">(</span><span class="token4">'itemclick'</span><span class="token1">,</span>item<span class="token1">)</span> <span class="token6">//自定义事件</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token6">// 2父组件</span> <span class="token2">const</span> app <span class="token">=</span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> message<span class="token1">:</span> <span class="token4">"测试数据"</span> <span class="token1">}</span><span class="token1">,</span> methods<span class="token1">:</span> <span class="token1">{</span> <span class="token3">cpnClick</span><span class="token1">(</span>item<span class="token1">)</span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token4">'父组件接收-->'</span><span class="token">+</span>item<span class="token1">.</span>name<span class="token1">)</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">,</span> <span class="token6">//注册子组件</span> components<span class="token1">:</span> <span class="token1">{</span> cpn <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ``` # 子组件访问父组件的内容 ``` <pre class="calibre14">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html lang<span class="token">=</span><span class="token4">"en"</span><span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"UTF-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Title<span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token1">{</span><span class="token1">{</span>message<span class="token1">}</span><span class="token1">}</span> <span class="token"><</span>cpn<span class="token">></span><span class="token"><</span><span class="token">/</span>cpn<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>template id<span class="token">=</span><span class="token4">"cpn"</span><span class="token">></span> <span class="token"><</span>div<span class="token">></span> <span class="token"><</span>h1<span class="token">></span>这是子组件标题<span class="token"><</span><span class="token">/</span>h1<span class="token">></span> <span class="token"><</span>p<span class="token">></span>这是子组件内容<span class="token"><</span><span class="token">/</span>p<span class="token">></span> <span class="token"><</span>button @click<span class="token">=</span><span class="token4">"btnClick"</span><span class="token">></span>获取父组件对象<span class="token"><</span><span class="token">/</span>button<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>template<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.jsdelivr.net/npm/vue/dist/vue.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token2">const</span> app <span class="token">=</span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> message<span class="token1">:</span> <span class="token4">"测试数据"</span> <span class="token1">}</span><span class="token1">,</span> components<span class="token1">:</span> <span class="token1">{</span> cpn<span class="token1">:</span><span class="token1">{</span> template<span class="token1">:</span><span class="token4">'#cpn'</span><span class="token1">,</span> methods<span class="token1">:</span> <span class="token1">{</span> <span class="token3">btnClick</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token2">this</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//console.log(this.$parent.message);</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token2">this</span><span class="token1">.</span>$root<span class="token1">.</span>message<span class="token1">)</span><span class="token1">;</span> <span class="token6">//建议采用这种方式 因为上面那种方式 如果嵌套多个组件 如果找不到name值 可能会报错</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ```