最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue中父子組件通信與事件觸發(fā)的深入講解

 更新時(shí)間:2022年03月22日 11:32:10   作者:小阿妍  
組件是Vue核心功能之一,合理的組件化,可以減少我們代碼的冗余,提高項(xiàng)目的可維護(hù)性,下面這篇文章主要給大家介紹了關(guān)于Vue中父子組件通信與事件觸發(fā)的相關(guān)資料,需要的朋友可以參考下

一、組件

子組件

<template>
  <div style="border:1px solid black;width:400px; height: 130px;">
    <h3>我是子組件</h3>
    <button>子組件將值傳遞給父組件</button>
    <div>子組件接收父組件的值:</div>
  </div>
</template> 

父組件

<template>
 <div style="border:1px solid red;padding:2rem;width:400px;margin:0 auto;">
    <h3>我是父組件</h3>
    <div>子組件向父組件傳遞的值:</div>
    <Child></Child>
  </div>
</template>

<script>
import Child from './Child';
export default {
    components: {
        Child
    }
}
</script>

效果展示:

通過這張圖可以看出父子組件的結(jié)構(gòu),下面我們來實(shí)習(xí)父子組件通信。

二、父子組件通信

父組件給子組件通信

實(shí)現(xiàn)思路:子組件通過 props 來接受父組件傳過來的值。

  • 在父組件中,定義一個(gè)data變量,在子組件標(biāo)簽中動(dòng)態(tài)綁定這個(gè)值。

    // Father.vue
    <template>
     <div style="border:1px solid red;padding:2rem;width:400px;margin:0 auto;">
        <h3>我是父組件</h3>
        <div>子組件向父組件傳遞的值:{{ChildMsg}}</div>
        <Child :FatherMsg="data"></Child>
      </div>
    </template>
    
    <script>
    import Child from './Child';
    export default {
        data() {
            return {
                data: 'I am your father',
            }
        },
        components: {
            Child
        }
    }
    </script>
  • 接著在子組件里通過 props 來接收,這樣子組件就接收到了父組件傳遞過來的值了。

    // Child.vue
    <template>
      <div style="border:1px solid black;width:400px; height: 130px;">
        <h3>我是子組件</h3>
        <button>子組件將值傳遞給父組件</button>
        <div>父組件向子組件傳遞的值:{{FatherMsg}}</div>
      </div>
    </template> 
    
    <script>
    export default {
        data() {
            return {
                data: 'I am your children',
            }
        },
        props: ['FatherMsg']
    }
    </script>

可以看到,我們父組件向子組件通信已經(jīng)實(shí)現(xiàn)了,接下來就是子組件向父組件通信了,這個(gè)就要使用到 this.$emit 方法了。

子組件向父組件通信

實(shí)現(xiàn)思路:通過在子組件中使用 this.$emit 來觸發(fā)自定義事件并傳值,然后在父組件中監(jiān)聽該事件即可。

  • 在子組件中給 button 按鈕添加 click 事件,來通過 this.$emit 自定義事件,并傳入一個(gè)參數(shù):

    <template>
      <div style="border:1px solid black;width:400px; height: 130px;">
        <h3>我是子組件</h3>
        <button @click="send">子組件將值傳遞給父組件</button>
        <div>父組件向子組件傳遞的值:{{FatherMsg}}</div>
      </div>
    </template> 
    
    <script>
    export default {
        data() {
            return {
                data: 'I am your children',
            }
        },
        props: ['FatherMsg'],
        methods: {
          send() {
            this.$emit('ListenChild', this.data);
          }
        }
    }
    </script>
  • 在父組件中的子組件標(biāo)簽里,先在 data 里定義一個(gè)變量接收這個(gè)值,然后監(jiān)聽在子組件中自定義的事件,并接受這個(gè)參數(shù)賦值給定義的變量:

    <template>
     <div style="border:1px solid red;padding:2rem;width:400px;margin:0 auto;">
        <h3>我是父組件</h3>
        <div>子組件向父組件傳遞的值:{{ChildMsg}}</div>
        <Child :FatherMsg="data" @ListenChild="ListenChild"></Child>
      </div>
    </template>
    
    <script>
    import Child from './Child';
    export default {
        data() {
            return {
                data: 'I am your father',
                ChildMsg: '',
            }
        },
        components: {
            Child
        },
        methods: {
            ListenChild(data) {
                console.log("子組件傳遞過來的值:" , data);
                this.ChildMsg = data;
            }
        }
    }
    </script>

點(diǎn)擊子組件中的“子組件將值傳遞給父組件”,就可看到如下效果:

三、父子組件事件觸發(fā)

父組件調(diào)用子組件中的事件方法

  • 通過 ref 直接調(diào)用子組件的方法:

    // Child.vue 
    <template>
      <div style="border: 1px solid black; width: 150px; margin: 10px auto">
        我是子組件
        <div style="color: red"> {{ msg }} </div>
      </div>
    </template>
    <script>
    export default {
        data() {
            return {
                msg: '',
            }
        },
      methods: {
        childFun() {
          console.log('我是子組件的方法 childFun');
          this.msg = '我的方法被調(diào)用了'
        },
      },
    };
    </script>

    在子組件標(biāo)簽上添加 ref 屬性,然后在方法中通過 this.$refs 找到綁定 ref 的屬性調(diào)用該子組件內(nèi)的方法即可。

    // Father.vue
    <template>
      <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto">
        我是父組件
        <Button @click="handleClick">點(diǎn)擊調(diào)用子組件方法</Button>
        <Child ref="child" />
      </div>
    </template>    
    
    <script>
    import Child from './Child';
    
    export default {
        components: {
            Child
        },
        methods: {
            handleClick() {
                this.$refs.child.childFun();
            },
        },
    }
    </script>
  • 通過組件的 $emit、$on 方法:

    // Child.vue 
    <template>
      <div style="border: 1px solid black; width: 150px; margin: 10px auto">
        我是子組件
        <div style="color: red"> {{ msg }} </div>
      </div>
    </template>
    <script>
    export default {
        data() {
            return {
                msg: '',
            }
        },
      mounted() {
        this.$on('childFun', function() {
            console.log('我是子組件方法');
            this.msg = '我的方法被調(diào)用了'
        });
      }
    };
    </script>

    在子組件中使用 $on 綁定一個(gè)方法,然后在父組件中通過 $emit 找到綁定 $on 上面的事件名即可,但是也需要 ref 的配合。

    // Father.vue
    <template>
      <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto">
        我是父組件
        <Button @click="handleClick">點(diǎn)擊調(diào)用子組件方法</Button>
        <Child ref="child" />
      </div>
    </template>    
    
    <script>
    import Child from './Child';
    
    export default {
        components: {
            Child
        },
        methods: {
            handleClick() {
            	//子組件$on中的名字
                this.$refs.child.$emit("childFun")    
            },
        },
    }
    </script>

兩種實(shí)現(xiàn)方式效果一致。

調(diào)用方法前:

調(diào)用方法后:

子組件調(diào)用父組件中的事件方法

  • 直接在子組件中通過 this.$parent 來調(diào)用父組件的方法

    // Father.vue
    <template>
      <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto" >
        我是父組件
        <Child></Child>
        <div style="color: red"> {{ msg }} </div>
      </div>
    </template>
    <script>
      import Child from './Child';
      export default {
          data() {
              return {
                  msg: ''
              }
          },
        components: {
          Child
        },
        methods: {
          fatherMethod() {
            console.log('我的父組件中的方法');
            this.msg = '我的方法被子組件調(diào)用了';
          }
        }
      };
    </script>
    // Child.vue
    <template>
      <div style="border: 1px solid black; width: 150px; margin: 10px auto">
        我是子組件
        <button @click="childMethod">點(diǎn)擊調(diào)用父組件方法</button>
      </div>
    </template>
    <script>
      export default {
        methods: {
          childMethod() {
            this.$parent.fatherMethod();
          }
        }
      };
    </script>
  • 在子組件里用 $emit 向父組件觸發(fā)一個(gè)事件,父組件監(jiān)聽這個(gè)事件(推薦使用)

    // Father.vue
    <template>
      <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto" >
        我是父組件
        <Child @fatherMethod="fatherMethod"></Child>
        <div style="color: red"> {{ msg }} </div>
      </div>
    </template>
    <script>
      import Child from './Child';
      export default {
          data() {
              return {
                  msg: ''
              }
          },
        components: {
          Child
        },
        methods: {
          fatherMethod() {
            console.log('我的父組件中的方法');
            this.msg = '我的方法被子組件調(diào)用了';
          }
        }
      };
    </script>

    子組件可以使用 $emit 觸發(fā)父組件的自定義事件。

    // Child.vue
    <template>
      <div style="border: 1px solid black; width: 150px; margin: 10px auto">
        我是子組件
        <button @click="childMethod">點(diǎn)擊調(diào)用父組件方法</button>
      </div>
    </template>
    <script>
      export default {
        methods: {
          childMethod() {
            // fatherMethod父組件方法
            this.$emit('fatherMethod'); 
          }
        }
      };
    </script>
  • 父組件把方法傳入子組件中,在子組件里直接調(diào)用這個(gè)方法:

    // Father.vue
    <template>
      <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto" >
        我是父組件
        <Child :fatherMethod="fatherMethod"></Child>
        <div style="color: red"> {{ msg }} </div>
      </div>
    </template>
    <script>
      import Child from './Child';
      export default {
          data() {
              return {
                  msg: ''
              }
          },
        components: {
          Child
        },
        methods: {
          fatherMethod() {
            console.log('我的父組件中的方法');
            this.msg = '我的方法被子組件調(diào)用了';
          }
        }
      };
    </script>

    父組件可以將事件綁定到子組件標(biāo)簽上,子組件使用 props 接收父組件的事件。

    // Child.vue
    <template>
      <div style="border: 1px solid black; width: 150px; margin: 10px auto">
        我是子組件
        <button @click="childMethod">點(diǎn)擊調(diào)用父組件方法</button>
      </div>
    </template>
    <script>
      export default {
        props: {
          fatherMethod: {
            type: Function,
            default: null
          }
        },
        methods: {
          childMethod() {
            if (this.fatherMethod) {
              this.fatherMethod();
            }
          }
        }
      };
    </script>

以上三種實(shí)現(xiàn)方式效果一致。

調(diào)用方法前:

調(diào)用方法后:

四、總結(jié)

至此,Vue 父子組件之間大部分的操作都涉及到了,我們?cè)诔绦虻拈_發(fā)過程中對(duì)于該部分內(nèi)容可以游刃有余了。

到此這篇關(guān)于Vue中父子組件通信與事件觸發(fā)的文章就介紹到這了,更多相關(guān)Vue父子組件通信與事件觸發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue-cli的webpack模板項(xiàng)目配置文件分析

    vue-cli的webpack模板項(xiàng)目配置文件分析

    本篇文章主要對(duì)vue-cli的webpack模板項(xiàng)目配置文件進(jìn)行分析。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • vue3使用vue-router嵌套多級(jí)路由的方法

    vue3使用vue-router嵌套多級(jí)路由的方法

    Vue3 嵌套路由的使用和 Vue2 相差不大,主要的區(qū)別是 Vue3 的路由實(shí)例化使用了 createApp() 方法,所以實(shí)例化路由時(shí)需要傳入根組件,這篇文章主要介紹了vue3使用vue-router嵌套路由(多級(jí)路由),需要的朋友可以參考下
    2023-12-12
  • Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼

    Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼

    本篇文章主要介紹了Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-07-07
  • vue循環(huán)中調(diào)用接口-promise.all();按順序執(zhí)行異步處理方式

    vue循環(huán)中調(diào)用接口-promise.all();按順序執(zhí)行異步處理方式

    這篇文章主要介紹了vue循環(huán)中調(diào)用接口-promise.all();按順序執(zhí)行異步處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue項(xiàng)目中如何安裝element組件

    Vue項(xiàng)目中如何安裝element組件

    這篇文章主要介紹了Vue項(xiàng)目中如何安裝element組件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • vue實(shí)現(xiàn)token過期自動(dòng)跳轉(zhuǎn)到登錄頁面

    vue實(shí)現(xiàn)token過期自動(dòng)跳轉(zhuǎn)到登錄頁面

    本文主要介紹了vue實(shí)現(xiàn)token過期自動(dòng)跳轉(zhuǎn)到登錄頁面,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory

    解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory

    這篇文章主要介紹了解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Element input樹型下拉框的實(shí)現(xiàn)代碼

    Element input樹型下拉框的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Element input樹型下拉框的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • Vue基于el-breadcrumb實(shí)現(xiàn)面包屑功能(操作代碼)

    Vue基于el-breadcrumb實(shí)現(xiàn)面包屑功能(操作代碼)

    這篇文章主要介紹了Vue基于el-breadcrumb實(shí)現(xiàn)面包屑功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • vue路由的配置和頁面切換詳解

    vue路由的配置和頁面切換詳解

    這篇文章主要給大家介紹了關(guān)于vue路由的配置和頁面切換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評(píng)論

安新县| 阿拉善盟| 托克托县| 吴旗县| 石棉县| 三穗县| 杭锦后旗| 宝清县| 明光市| 田东县| 永靖县| 西丰县| 图木舒克市| 高雄市| 乌拉特前旗| 锦州市| 哈巴河县| 徐闻县| 班玛县| 融水| 黑山县| 武山县| 鄂伦春自治旗| 沙坪坝区| 从江县| 齐齐哈尔市| 林芝县| 九江市| 石泉县| 德兴市| 嘉定区| 桂阳县| 寿宁县| 罗江县| 武川县| 揭西县| 安远县| 叙永县| 哈巴河县| 湖州市| 简阳市|