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

深入了解Vue3組件傳值方式

 更新時(shí)間:2022年07月09日 14:08:09   作者:ed。  
學(xué)習(xí)過?vue2?的寶子們肯定知道,組件傳值是?vue?項(xiàng)目開發(fā)過程中必不可少的功能場景,在?vue2?里面有很多傳值的方式。今天就來和大家講講Vue3的組件傳值方式,需要的可以參考一下

今天說一下 vue3 的組件間傳值,學(xué)習(xí)過 vue2 的寶子們肯定知道,組件傳值是 vue 項(xiàng)目開發(fā)過程中必不可少的功能場景,在 vue2 里面有很多傳值的方式,vue3 的傳值方式呢,在這里稍微整理總結(jié)一下,但是不是很全,后期可能慢慢補(bǔ)充。

父子組件傳值 props

和 vue2 一樣,vue3 也可以使用 props 進(jìn)行父組件傳值給子組件,這個(gè)就不多說了直接上代碼。

父組件

<template>
  <div>
    <div class="father">
      <h1>這是父組件</h1>
      <h2>父組件的名字:{{boy.name}}</h2>
    </div>
    <hello-world :msg="msg" :boy="boy" @change="btn"></hello-world>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

import { ref, reactive } from "vue";

export default {
  name: "App",
  components: {
    HelloWorld
  },
  setup() {
    const boy = reactive({
      name: "我是????.",
      age: 10
    });
    const msg = ref("這是一條信息");

    const btn = val => {
      console.log(val);
      alert(val);
    };

    return { boy, msg, btn };
  }
};
</script>

<style scoped>
.father {
  background-color: aquamarine;
}
</style>

子組件

<template>
  <div class="son">
    <h1>這是子組件</h1>
    <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{msg}}</h2>
    <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.name}}</h2>
    <button @click="btn">傳給父組件數(shù)據(jù)</button>
  </div>
</template>

<script>
import { toRefs } from "vue";
export default {
  name: "HelloWorld",
  props: {
    msg: String,
    boy: Object
  },
  setup(props, { emit }) {
    console.log(props);
    const p = toRefs(props);
    const msg = p.msg;
    const boy = p.boy;

    const btn = () => {
      const news = "我是子組件傳進(jìn)的值";
      emit("change", news);
    };

    return { msg, boy, btn };
  }
};
</script>

<style scoped>
.son {
  background-color: bisque;
}
</style>

保存查看效果。

上面就是 props 傳值的基本用法。

祖孫組件傳值 provide 和 inject

這個(gè)其實(shí)和 vue2 的寫法是一模一樣的。

直接上代碼!!

父組件

<template>
  <div>
    <div class="father">
      <h1>這是父組件</h1>
      <h2>名字:{{boy.name}}</h2>
      <h2>年齡:{{boy.age}}</h2>
    </div>
    <hello-world></hello-world>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

import { reactive, provide } from "vue";

export default {
  name: "App",
  components: {
    HelloWorld
  },
  setup() {
    const boy = reactive({
      name: "我是????.",
      age: 10
    });

    provide("boy", boy); // 往子孫組件傳值

    return { boy };
  }
};
</script>

<style scoped>
.father {
  background-color: aquamarine;
}
</style>

子組件

<template>
  <div class="son">
    <h1>這是子組件</h1>
    <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.name}}</h2>
    <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.age}}</h2>
  </div>
</template>

<script>
import { toRefs, inject } from "vue";
export default {
  name: "HelloWorld",
  setup() {
    const boy = inject("boy"); // 子孫組件接收值
    return { boy };
  }
};
</script>

<style scoped>
.son {
  background-color: bisque;
}
</style>

刷新看一下效果。

好的,我們可以看到子組件可以順利拿到父組件傳進(jìn)來的數(shù)據(jù)值。

父組件中點(diǎn)擊按鈕向子組件傳值

就是使用 ref 來獲取dom 然后操作里面的參數(shù)和方法。

父組件

<template>
  <div>
    <div class="father">
      <h1>這是父組件</h1>
      <h2>名字:{{boy.name}}</h2>
      <h2>年齡:{{boy.age}}</h2>
      <button @click="btn">傳值</button>
    </div>
    <hello-world ref="hello" style="color: red"></hello-world>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

import { reactive, ref } from "vue";

export default {
  name: "App",
  components: {
    HelloWorld
  },
  setup() {
    const boy = reactive({
      name: "我是????.",
      age: 10
    });

    const hello = ref();

    function btn() {
      hello.value.init(boy); // 調(diào)用子組件的方法,把boy對(duì)象傳進(jìn)去
    }

    return { boy, btn, hello };
  }
};
</script>

<style scoped>
.father {
  background-color: aquamarine;
}
</style>

子組件

<template>
  <div class="son">
    <h1>這是子組件</h1>
    <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.name}}</h2>
    <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.age}}</h2>
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
export default {
  name: "HelloWorld",
  setup() {
    let boy = reactive({
      name: "ed.",
      age: 11
    });

    // 提供給父組件調(diào)用的方法
    const init = val => {
      boy.name = val.name;
      boy.age = val.age;
    };

    return { init, boy };
  }
};
</script>

<style scoped>
.son {
  background-color: bisque;
}
</style>

以上就是深入了解Vue3組件傳值方式的詳細(xì)內(nèi)容,更多關(guān)于Vue3組件傳值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue?頁面刷新、重置、更新頁面所有數(shù)據(jù)的示例代碼

    vue?頁面刷新、重置、更新頁面所有數(shù)據(jù)的示例代碼

    Vue.js提供了多種方式來實(shí)現(xiàn)頁面刷新、重置和更新頁面所有數(shù)據(jù)的功能,下面通過示例代碼演示vue?頁面刷新、重置、更新頁面所有數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • Vue組件通信入門之Provide和Inject機(jī)制

    Vue組件通信入門之Provide和Inject機(jī)制

    這篇文章主要給大家介紹了關(guān)于Vue組件通信入門之Provide和Inject機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue組件通信具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • cdn模式下vue的基本用法詳解

    cdn模式下vue的基本用法詳解

    這篇文章主要介紹了cdn模式下vue的基本用法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-10-10
  • 基于vue+openlayer實(shí)現(xiàn)地圖聚合和撒點(diǎn)效果

    基于vue+openlayer實(shí)現(xiàn)地圖聚合和撒點(diǎn)效果

    Openlayers 是一個(gè)模塊化、高性能并且功能豐富的WebGIS客戶端的JavaScript包,用于顯示地圖及空間數(shù)據(jù),并與之進(jìn)行交互,具有靈活的擴(kuò)展機(jī)制,本文給大家介紹vue+openlayer實(shí)現(xiàn)地圖聚合效果和撒點(diǎn)效果,感興趣的朋友一起看看吧
    2021-09-09
  • vue實(shí)現(xiàn)點(diǎn)擊選中,其他的不選中方法

    vue實(shí)現(xiàn)點(diǎn)擊選中,其他的不選中方法

    今天小編就為大家分享一篇vue實(shí)現(xiàn)點(diǎn)擊選中,其他的不選中方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue的狀態(tài)管理模式vuex

    vue的狀態(tài)管理模式vuex

    本篇文章主要介紹了深入理解vue的狀態(tài)管理模式vuex,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • Vue使用echarts繪制柱狀圖和折線圖

    Vue使用echarts繪制柱狀圖和折線圖

    這篇文章主要為大家學(xué)習(xí)介紹了Vue如何使用echarts繪制柱狀圖和折線圖,文中有詳細(xì)的示例代碼,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08
  • vue和webpack項(xiàng)目構(gòu)建過程常用的npm命令詳解

    vue和webpack項(xiàng)目構(gòu)建過程常用的npm命令詳解

    本文通過實(shí)例代碼給大家介紹了vue和webpack項(xiàng)目構(gòu)建過程常用的npm命令,需要的朋友可以參考下
    2018-06-06
  • vue綁定數(shù)字類型 value為數(shù)字的實(shí)例

    vue綁定數(shù)字類型 value為數(shù)字的實(shí)例

    這篇文章主要介紹了vue綁定數(shù)字類型 value為數(shù)字的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue如何根據(jù)條件判斷按鈕是否可以點(diǎn)擊

    vue如何根據(jù)條件判斷按鈕是否可以點(diǎn)擊

    這篇文章主要介紹了vue如何根據(jù)條件判斷按鈕是否可以點(diǎn)擊,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評(píng)論

自贡市| 泗洪县| 普安县| 武功县| 淮滨县| 延吉市| 曲沃县| 威海市| 台北市| 宁化县| 苗栗市| 五大连池市| 崇州市| 灵武市| 苏尼特右旗| 南投县| 嵊州市| 治县。| 石河子市| 水富县| 宜阳县| 瓮安县| 普兰县| 交城县| 乌苏市| 抚宁县| 无为县| 新晃| 岳池县| 雅江县| 东城区| 高尔夫| 五寨县| 南江县| 田东县| 河北区| 庆城县| 双辽市| 合肥市| 石首市| 杂多县|