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

一文搞懂Vue2中的組件通信

 更新時間:2022年07月28日 11:32:46   作者:lupulus  
這篇文章主要為大家介紹了Vue2中的組件通信方式,文中通過示例進(jìn)行了詳細(xì)的介紹,對我們學(xué)習(xí)Vue有一定的幫助,感興趣的小伙伴可以了解一下

vue 組件通信方式

1.父組件將自己的狀態(tài)分享給子組件使用;

方法:父組件通過子標(biāo)簽傳遞數(shù)據(jù),子組件通過 props 接收

2.子組件改變父組件的狀態(tài);

方法:父組件在子標(biāo)簽上通過@abc 提供一個改變自身狀態(tài)的方法,子組件通過$emit("abc", payload)觸發(fā)這個函數(shù)

3.父組件直接改變子組件的狀態(tài);

方法:父組件設(shè)法($ref,$children[0])拿到子組件實例對象,然后通過實例對象直接修改子組件狀態(tài)

4.子組件直接改變父組件的狀態(tài)

方法:子組件通過$parent拿到父組件的改變自身狀態(tài)的方法,然后直接調(diào)用($parent 可以拿到父組件狀態(tài),但是最好不要直接修改,而是通過父組件函數(shù)式修改,保持單向數(shù)據(jù)流)

5.父組件通過插槽向子組件傳遞數(shù)據(jù)

方法:子組件定義具名插槽,父組件向插槽內(nèi)傳遞自己的狀態(tài)

6.父組件向后代組件傳值

方法:父組件正常在標(biāo)簽上向子組件傳遞數(shù)據(jù),子組件不用 props 接收屬性,通過$attrs獲取屬性,通過$listeners 獲取方法。子組件再向下傳遞時,使用 v-bind="$attr"傳遞屬性,使用v-on="$listeners"傳遞方法

7.父組件向后代組件傳值

方法:父組件js中定義provide函數(shù)提供數(shù)據(jù)源,后代組件通過inject去接收,inject可以是一個數(shù)組或?qū)ο蟆?/p>

注意:父組件提供的數(shù)據(jù)源如果不是響應(yīng)式的,那么后代修改數(shù)據(jù),數(shù)據(jù)不會響應(yīng)變化。如果父組件提供的數(shù)據(jù)源是響應(yīng)式的,但是不是一個對象,那么它也不是響應(yīng)式的,所以要用對象在外包一層對象(數(shù)組不行)。另外,如果子組件同時provide一個inject祖先組件相同名稱的數(shù)據(jù),那么子組件的后代會就近使用子組件的數(shù)據(jù)。

官網(wǎng)tip:provide 和 inject 綁定并不是可響應(yīng)的。這是刻意為之的。然而,如果你傳入了一個可監(jiān)聽的對象,那么其對象的 property 還是可響應(yīng)的。

8.全局通信-事件總線

方法:通過注冊一個新的vue實例作為橋梁,使用$on和$emit來完成通信

9.全局通信-vuex

略(看官方文檔嘍)

1.props傳參

// 父組件向子組件傳遞msg
<template>
  <div>
    <p>我是dad</p>
    <Child :msg="msg" />
  </div>
</template>

<script>
import Child from "./ChildItem.vue";

export default {
  name: "App",
  components: {
    Child,
  },
  data() {
    return {
      msg: "父親的忠告",
    };
  },
};
</script>

// 子組件props接收
<template>
  <div>
    <p>我是子組件</p>
    <span>{{ msg }}</span>
  </div>
</template>

<script>
export default {
  props: {
    msg: {
      type: String,
      default: "什么都沒有",
    },
  },
};
</script>

2.emit,on通信

// 父組件向子組件提供改變自己狀態(tài)的函數(shù)
<template>
  <div>
    <p>我是dad</p>
    <Child @changeMyMind="changeMyMind" />
    <span>{{ mind }}</span>
  </div>
</template>

<script>
import Child from "./ChildItem.vue";

export default {
  name: "App",
  components: {
    Child,
  },
  data() {
    return {
      mind: "偉大萬歲",
    };
  },
  methods: {
    changeMyMind(yourMind) {
      this.mind = yourMind;
    },
  },
};
</script>


// 子組件不用接收,直接通過$emit觸發(fā)并傳參就行
<template>
  <div>
    <p>我是子組件</p>
  </div>
</template>

<script>
export default {
  mounted() {
    this.$emit("changeMyMind", "躺平鳥");
  },
};
</script>

3.$ref,$children實例通信

// 父組件通過$ref或者$children拿到子組件實例,然后直接修改子組件狀態(tài)
/**
 * this.$children`數(shù)組中的元素不是響應(yīng)式的,并且下標(biāo)并不一定對用父組件引用的子組件的順序,例如有異步加載的子組件,可能影響其在 children 數(shù)組中的順序。所以使用時需要根據(jù)一定的條件例如子組件的name去找到相應(yīng)的子組件。
 **/
<template>
  <div>
    <p>我是dad</p>
    <Child ref="childRef" />
  </div>
</template>

<script>
import Child from "./ChildItem.vue";

export default {
  components: {
    Child,
  },
  mounted() {
    // this.$children[0].childMsg = "不你不是";
    this.$refs.childRef.childMsg = "不你不是";
  },
};
</script>



// 子組件等著被改就行
<template>
  <div>
    <p>我是子組件</p>
    <span>{{ childMsg }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      childMsg: "我是子組件數(shù)據(jù)",
    };
  },
};
</script>

4.$parent通信

// 父組件
<template>
  <div>
    <p>我是dad</p>
    <Child />
    <span>{{ aa }}</span>
  </div>
</template>

<script>
import Child from "./ChildItem.vue";

export default {
  components: {
    Child,
  },
  data() {
    return {
      aa: "",
    };
  },
  methods: {
    changeAa(data) {
      this.aa = data;
    },
  },
};
</script>

// 子組件通過$parent拿到父組件實例,然后直接修改父組件狀態(tài)
<template>
  <div>
    <p>我是子組件</p>
    <span>{{ childMsg }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      childMsg: "我是子組件數(shù)據(jù)",
    };
  },
  mounted() {
    // this.$parent.aa = "我改了哈"; 不推薦
    this.$parent.changeAa("我改了哦");
  },
};
</script>

5.插槽通信(一般不用)

// 父組件
<template>
  <div>
    <p>我是dad</p>
    <Child>
      <template v-slot:boring>
        {{ aa }}
      </template>
    </Child>
  </div>
</template>

<script>
import Child from "./ChildItem.vue";

export default {
  components: {
    Child,
  },
  data() {
    return {
      aa: "父組件的數(shù)據(jù)哦",
    };
  },
};
</script>


// 子組件定義插槽
<template>
  <div>
    <p>我是子組件</p>
    <slot name="boring"></slot>
  </div>
</template>

<script>
export default {};
</script>

6.$attr,$listener深層雙向通信

// 父組件
<template>
  <div>
    <p>我是dad</p>
    <span>{{ dadData }}</span>
    <Son :dadData="dadData" @changeDadData="changeDadData" @keyup="someKeyUp" />
  </div>
</template>

<script>
import Son from "./SonItem.vue";

export default {
  components: {
    Son,
  },
  data() {
    return {
      dadData: "父組件的數(shù)據(jù)哦",
    };
  },
  methods: {
    changeDadData(newData) {
      this.dadData = newData;
    },
    someKeyUp(e) {
      console.log(e.target.value);
    },
  },
};
</script>



// 兒子組件
<template>
  <div>
    <p>我是兒子組件</p>
    <span>{{ $attrs.dadData }}</span>
    <input type="text" v-on="$listeners" />
    <GrandSon v-bind="$attrs" v-on="$listeners" />
  </div>
</template>

<script>
import GrandSon from "./GrandSon.vue";

export default {
  components: {
    GrandSon,
  },
  mounted() {
    console.log(this.$listeners);
  },
};
</script>

// 孫子組件
<template>
  <div>
    <p>我是孫子組件</p>
    <input type="text" @input="grandsonInput" />
  </div>
</template>

<script>
export default {
  methods: {
    grandsonInput(e) {
      //   this.$emit("changeDadData", e.target.value); 也可以觸發(fā)
      this.$listeners.changeDadData(e.target.value);
    },
  },
};
</script>

7.provide,inject依賴注入深層次單向通信

// 父組件
<template>
  <div>
    <p>我是dad</p>
    <span>{{ dadMessage }}</span>
    <Son />
  </div>
</template>

<script>
import Son from "./SonItem.vue";

export default {
  components: {
    Son,
  },
  provide() {
    return {
      message: this.dadMessage,
    };
  },
  data() {
    return {
      dadMessage: {
        textContent: "我是個祖先數(shù)據(jù)",
      },
    };
  },
};
</script>


// 兒子組件
<template>
  <div>
    <p>我是兒子組件</p>
    <span>{{ theData }}</span>
    <GrandSon />
  </div>
</template>

<script>
import GrandSon from "./GrandSon.vue";

export default {
  components: {
    GrandSon,
  },
  inject: {
    // 起個別名
    theData: {
      // 數(shù)據(jù)來源映射
      from: "message",
      // 默認(rèn)值
      default: () => ({ message: { textContent: "啥也不是" } }),
    },
  },
};
</script>


// 孫子組件
<template>
  <div>
    <p>我是孫子組件</p>
    <input type="text" @input="grandsonInput" />
    <span>{{ message.textContent }}</span>
  </div>
</template>

<script>
export default {
  methods: {
    grandsonInput(e) {
      this.message.textContent = e.target.value;
    },
  },
  inject: ["message"],
};
</script>

8.$bus事件總線全局通信

// 父組件通過this.$bus.$on監(jiān)聽事件
<template>
  <div>
    <p>我是dad</p>
    <span>{{ dadData }}</span>
    <Son />
  </div>
</template>

<script>
import Son from "./SonItem.vue";

export default {
  components: {
    Son,
  },
  data() {
    return {
      dadData: "我是爹地",
    };
  },
  mounted() {
    this.$bus.$on("changeDadData", this.changeDadData);
  },
  methods: {
    changeDadData(newData) {
      this.dadData = newData;
    },
  },
  // 記得清除監(jiān)聽
  beforeDestroy() {
    this.$bus.$off("changeDadData");
  },
};
</script>


// 孫子組件通過this.$bus.$emit觸發(fā)事件
<template>
  <div>
    <p>我是孫子組件</p>
    <input type="text" @input="grandsonInput" />
    <span></span>
  </div>
</template>

<script>
export default {
  methods: {
    grandsonInput(e) {
      this.$bus.$emit("changeDadData", e.target.value);
    },
  },
};
</script>

到此這篇關(guān)于一文搞懂Vue2中的組件通信的文章就介紹到這了,更多相關(guān)Vue2組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新

    詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新

    這篇文章主要介紹了詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • 關(guān)于vue-socket.io使用及版本原因消息無法監(jiān)聽bug

    關(guān)于vue-socket.io使用及版本原因消息無法監(jiān)聽bug

    這篇文章主要介紹了關(guān)于vue-socket.io使用及版本原因消息無法監(jiān)聽bug,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 在Vue中使用mockjs代碼實例

    在Vue中使用mockjs代碼實例

    這篇文章主要介紹了在Vue中使用mockjs代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • vue-router跳轉(zhuǎn)頁面的方法

    vue-router跳轉(zhuǎn)頁面的方法

    本篇文章主要介紹了vue-router跳轉(zhuǎn)頁面的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • vue.js如何更改默認(rèn)端口號8080為指定端口的方法

    vue.js如何更改默認(rèn)端口號8080為指定端口的方法

    本篇文章主要介紹了vue.js如何更改默認(rèn)端口號8080為指定端口的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • vue.js打包之后可能會遇到的坑!

    vue.js打包之后可能會遇到的坑!

    這篇文章主要給大家介紹了關(guān)于vue.js打包之后可能會遇到的一些坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • Vue-admin-template?添加、跳轉(zhuǎn)子頁面問題

    Vue-admin-template?添加、跳轉(zhuǎn)子頁面問題

    這篇文章主要介紹了Vue-admin-template?添加、跳轉(zhuǎn)子頁面問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • react里組件通信的幾種方式小結(jié)

    react里組件通信的幾種方式小結(jié)

    本文主要介紹了react里組件通信的幾種方式小結(jié),包含了5種方式,主要是props傳遞,回調(diào)函數(shù)作為props,Context,Redux或MobX,refs,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • Vue組件更新數(shù)據(jù)v-model不生效的解決

    Vue組件更新數(shù)據(jù)v-model不生效的解決

    這篇文章主要介紹了Vue組件更新數(shù)據(jù)v-model不生效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue2.x中利用@font-size引入字體圖標(biāo)報錯的解決方法

    Vue2.x中利用@font-size引入字體圖標(biāo)報錯的解決方法

    今天小編就為大家分享一篇Vue2.x中利用@font-size引入字體圖標(biāo)報錯的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評論

准格尔旗| 龙游县| 乐安县| 东海县| 伊吾县| 法库县| 济源市| 永福县| 中卫市| 河曲县| 梅河口市| 确山县| 石渠县| 资阳市| 道真| 陇南市| 松阳县| 漳平市| 宣城市| 漳浦县| 营口市| 唐海县| 泾源县| 陇西县| 宽城| 石棉县| 正阳县| 孟津县| 嘉兴市| 连南| 镇雄县| 定安县| 茌平县| 崇礼县| 清镇市| 诸暨市| 扶风县| 安福县| 航空| 渝中区| 农安县|