VUE3中的組件傳值

一、父傳子(props)

在子組件中可以使用defineProps接收父組件向子組件的傳值

父組件fatherPage.vue:

<template><div class="father"><button @click="a = a + 1">按鈕</button><childPage :a="a" /></div>
</template><script lang="ts" setup>
import { ref } from "vue";
import childPage from "./childPage.vue";
const a = ref<number>(0);
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

子組件childPage.vue:

<template><div class="childPage">{{ a }}</div>
</template>
<script lang="ts" setup>
import { computed, defineProps } from "vue";
// 含默認值
// const porps = defineProps({
//   a: {
//     type: Number,
//     default: 0,
//   },
// });
const props = defineProps({ a: Number });
// 用computed使用props
const a = computed(() => props.a);
</script>
<style scoped lang="less">
.childPage {width: 100px;height: 60px;background-color: rgba(0, 0, 0, 0);display: flex;justify-content: center;align-items: center;color: #ffffff;
}
</style>

效果:

二、子傳父(emits)

在子組件中可以使用defineEmits向父組件傳遞信息。

父組件fatherPage.vue:

<template><div class="father">{{ a }}<childPage @update:a="update" /></div>
</template><script lang="ts" setup>
import { ref } from "vue";
import childPage from "./childPage.vue";
const a = ref<number>(0);
const update = (emitValue: number) => {a.value = emitValue;
};
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

子組件childPage.vue:

1.通過點擊觸發emit傳值

<template><div class="childPage"><button @click="updateA">按鈕</button></div>
</template><script lang="ts" setup>
import { ref, defineEmits } from "vue";const a = ref<number>(0);
const emit = defineEmits(["update:a"]);
const updateA = () => {a.value++;emit("update:a", a.value);
};
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

2.通過watch監聽值的變化進行傳值:

<template><div class="childPage"><button @click="updateA">按鈕</button></div>
</template><script lang="ts" setup>
import { ref, defineEmits, watch } from "vue";const a = ref<number>(0);
const emit = defineEmits(["update:a"]);
const updateA = () => {a.value++;setTimeout(() => {updateA();}, 1000);
};
watch(a, (newValue) => {emit("update:a", newValue);
});
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

三、父子組件進行雙向綁定(v-model)

在子組件中可以使用defineModel與父組件進行雙向綁定。注意:defineModel在vue3.3版本實驗性使用,穩定版在vue3.4。

父組件:

<template><div class="father">{{ a }}<childPage v-model="a" /></div>
</template><script lang="ts" setup>
import { ref } from "vue";
import childPage from "./childPage.vue";
const a = ref<number>();
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

子組件:

<template><div class="childPage"><button @click="updateA">按鈕</button></div>
</template><script lang="ts" setup>
import { defineModel } from "vue";const a = defineModel({type: Number,default: 0,
});const updateA = () => {a.value += 1;
};
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

四、使用vuex進行組件通信

使用vuex可以進行組件之間的通信,父子組件與兄弟組件均可

在vuex文件index.ts中:

import { createStore } from "vuex";export default createStore({state: {number: 0,},getters: {},mutations: {changeNumber(state, payload) {state.number = payload;},},actions: {},modules: {},
});

在第一個組件HomeView中:

<template><div class="father">{{ a }}<AboutView /></div>
</template><script lang="ts" setup>
import { computed } from "vue";
import { useStore } from "vuex";
import AboutView from "./AboutView.vue";
const store = useStore();
const a = computed(() => store.state.number);
</script>
<style scoped lang="scss">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;
}
</style>

在第二個組件AboutView中:

<template><div class="childPage"><button @click="updateA">按鈕</button></div>
</template><script lang="ts" setup>
import { useStore } from "vuex";const store = useStore();
let a = 0;const updateA = () => {a++;store.commit("changeNumber", a);
};
</script><style scoped lang="scss">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

五、使用pinia進行組件通信

pinia有著與vuex類似的功能,同樣可以實現組件間的通信。

在store的index.ts文件中:

import { defineStore } from "pinia";
import { ref } from "vue";
export const useCounterStore = defineStore("counter", () => {const count = ref(0);const changeCount = () => {count.value++;};return { count, changeCount };
});

在第一個組件fatherPage中:

<template><div class="father">{{ count }}<childPage /></div>
</template><script lang="ts" setup>
import childPage from "./childPage.vue";
import { useCounterStore } from "../store/index";
import { storeToRefs } from "pinia";
const store = useCounterStore();
const { count } = storeToRefs(store);
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

在第二個組件childPage中:

<template><div class="childPage"><button @click="changeCount">按鈕</button></div>
</template><script lang="ts" setup>
import { useCounterStore } from "../store/index";
const store = useCounterStore();
const { changeCount } = store;
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

六、使用事件總線進行組件通信

我們可以利用第三方庫比如說mitt以事件總線的方式實現傳值

在mitt的index.ts中:

import mitt from "mitt";
const emitter = mitt();
export default emitter;

在第一個組件fatherPage中:

<template><div class="father">{{ count }}<childPage /></div>
</template><script setup>
import childPage from "./childPage.vue";
import emitter from "@/mitt/index";
import { ref } from "vue";
const count = ref(0);
emitter.on("changeCount", (counter) => {count.value = counter;
});
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

在第二個組件childPage中:

<template><div class="childPage"><button @click="changeCount">按鈕</button></div>
</template><script setup>
import emitter from "../mitt/index";
let a = 0;
const changeCount = () => {a++;emitter.emit("changeCount", a);
};
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

七、更詳細使用方式請參考以下文檔

1,Vue.js - 漸進式 JavaScript 框架 | Vue.js

2,開始 | Vuex

3,Pinia | Pinia 中文手冊 - 官網同步更新 v2.0.28

4,GitHub - developit/mitt: 🥊 Tiny 200 byte functional event emitter / pubsub.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/714513.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/714513.shtml
英文地址,請注明出處:http://en.pswp.cn/news/714513.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

clickhouse的多路徑存儲策略

存儲策略 clickhouse從19.15開始&#xff0c;MergeTree實現了自定義存儲策略的功能&#xff1a; JBOD策略&#xff1a;這種策略適合服務器掛多磁盤但沒做raid的場景。JBOD是一種輪詢策略&#xff0c;每次執行INSERT或者MERGE&#xff0c;所以產生的新分區會輪詢寫入各個磁盤。…

C#面:Application , Cookie 和 Session 會話有什么不同

Application、Cookie 和 Session 是在Web開發中常用的三種會話管理方式 Application&#xff08;應用程序&#xff09;&#xff1a; Application 是在服務器端保存數據的一種方式&#xff0c;它可以在整個應用程序的生命周期內共享數據。Application 對象是在應用程序啟動時創…

Nginx 隱藏版本信息和logo

1.隱藏版本信息 http {### 隱藏版本號 server_tokens off; } 2.隱藏圖標 2.1 cd nginx 安裝的路徑 cd/XXXX/nginx-1.2.0 2.2 編輯文件 vim src/core/nginx.h 修改define nginx_ver 中的內容 vim src/http/ngx_http_special_response.c 修改 u_char ngx_http_error_tail[]…

java 基礎(核心知識搭配代碼)

前言 java的學習分為了上部分以及下部分進行學習&#xff0c;上部分就是對于java的基礎知識&#xff0c;面向對象上&#xff0c;面向對象下&#xff0c;異常操作&#xff0c;javaApi&#xff1b;下部主要是集合&#xff0c;泛型&#xff0c;反射&#xff0c;IO流&#xff0c;J…

Grid-Based Continuous Normal Representation for Anomaly Detection 論文閱讀

Grid-Based Continuous Normal Representation for Anomaly Detection 論文閱讀 摘要簡介方法3.1 Normal Representation3.2 Feature Refinement3.3 Training and Inference 4 實驗結果5 總結 文章信息&#xff1a; 原文鏈接&#xff1a;https://arxiv.org/abs/2402.18293 源碼…

ChatGPT4.0使用次數限制解讀

ChatGPT4.0使用次數限制解讀 ChatGPT4.0簡介 ChatGPT4.0&#xff0c;由OpenAI開發的先進通用聊天機器人模型&#xff0c;基于GPT4技術構建&#xff0c;為用戶提供了自然語言處理等多項任務的解決方案。 ChatGPT4.0使用次數限制 在日常使用過程中&#xff0c;用戶會遇到Chat…

【MIT 6.S081】2020, 實驗記錄(6),Lab: Copy-on-Write Fork

目錄 Task: Implement copy-on writestep 1&#xff1a;對內存塊進行引用計數step 2&#xff1a;uvmcopy 實現 fork 時將 parent 的物理頁映射到 child 中step 3&#xff1a;在 usertrap 中增加對 page fault 的處理執行測試 官方說明&#xff1a;Lab: Copy-on-Write Fork for …

IP地址工具,判斷IP是否在指定范圍內(支持ipv6)

常用方法&#xff0c;判斷一個ip是否在指定的ip范圍內&#xff0c;范圍可能包括起始ip范圍或者掩碼形式&#xff0c;無其它依賴&#xff0c; package com.yk.ip;import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; import jav…

操作系統-文件原理

目錄 一、磁盤 1.1 磁盤結構 1. 盤片&#xff1a; 2. 盤面&#xff1a; 3. 磁頭&#xff1a; 4. 磁道&#xff1a; 5. 扇區&#xff1a; 6. 磁道密度和扇區密度&#xff1a; 1.2 磁盤訪問 1. 尋道&#xff08;Seeking&#xff09;&#xff1a; 2. 延遲旋轉&#xff…

C++進階-- map和set

關聯式容器 在前面&#xff0c;我們所學的vector、list、deque&#xff0c;這些都是序列容器&#xff0c;也就是底層為線性序列的數據結構。 而關聯式容器是C標準庫中的一種類別&#xff0c;用于存儲鍵值對&#xff08;key-value pair&#xff09;&#xff0c;關聯式容器中的元…

vxe-table編輯單元格動態插槽slot的使用

業務場景&#xff1a;表格中只有特定某一行的的單元格可以編輯&#xff0c;列很多&#xff0c;為每個列寫個插槽要寫很多重復代碼&#xff0c;所以這里使用動態插槽&#xff0c;簡化代碼量。顯示編輯圖標&#xff0c;點擊編輯圖標隱藏。失去焦點保存調后臺接口。 解決辦法&…

Linux多線程服務端編程:使用muduo C++網絡庫 學習筆記 附錄C 關于Boost的看法

這是作者為電子工業出版社出版的《Boost程序庫完全開發指南》寫的推薦序&#xff0c;此處節選了作者對在C工程項目中使用Boost的看法。 最近一年&#xff08;這篇文章寫于2010年8月&#xff09;作者電話面試了數十位C應聘者。慣用的暖場問題是“工作中使用過STL的哪些組件&…

十行代碼開發一個AI應用

Semantic Kernal 簡介 Semantic Kernel (SK) is a lightweight SDK that lets you easily mix conventional programming languages with the latest in Large Language Model (LLM) AI "prompts" with templating, chaining, and planning capabilities out-of-the-…

關于vue中關于eslint報錯的問題

1 代碼保存的時候會自動將單引號報錯為雙引號 導致eslint報錯的問題&#xff0c; 解決思路&#xff1a; 在項目根目錄下新建一個.prettierrc.json文件 { “tabWidth”: 2,“useTabs”: false,“singleQuote”: true,“semi”: false} 2 關于報錯代碼的時候 出現尾隨逗號報錯…

Zabbix 系統告警“More than 75% used in the configuration cache”處理辦法

Zabbix系統報錯提示 Zabbix 系統告警“More than 75% used in the configuration cache”&#xff0c;看了一下意思是可用的配置緩存超過75%。 修改緩存大小 vim /etc/zabbix/zabbix_server.confesc : /CacheSize 找到配置 將64M改大一點&#xff0c;保存退出。 重啟zabbix…

WPF常用mvvm開源框架介紹 vue的mvvm設計模式鼻祖

WPF&#xff08;Windows Presentation Foundation&#xff09;是一個用于構建桌面應用程序的.NET框架&#xff0c;它支持MVVM&#xff08;Model-View-ViewModel&#xff09;架構模式來分離UI邏輯和業務邏輯。以下是一些常用的WPF MVVM開源框架&#xff1a; Prism Prism是由微軟…

代碼隨想錄算法訓練營 Day32 | LeetCode122.買賣股票的最佳時機II、LeetCode55. 跳躍游戲、LeetCode45.跳躍游戲II

LeetCode122.買賣股票的最佳時機II 那么這里面根據貪心思想&#xff1a; 1、在最低點時買入&#xff0c;如果該點比上一點價格更低&#xff0c;只有兩種情況&#xff1a;上一點為買入點&#xff0c;則此時更新買入點&#xff1b;上一點不是買入點&#xff0c;則此時將股票賣出…

物聯網的核心技術有哪些?

物聯網的核心技術有哪些? 說起物聯網的相關技術&#xff0c;涉及到很多&#xff0c;比如自動識別技術、傳感器技術、網絡通信技術、云計算等&#xff0c;但說到核心技術&#xff0c;我認為有以下6個。這6個核心技術分別是感知和識別技術、物聯網設備硬件、通信技術、數據處理技…

【MySQL】數據庫中常用的函數

目錄 聚合函數COUNT()函數的多種用法COUNT(*)COUNT(主鍵)COUNT(1)COUNT(常量)COUNT(非主鍵)COUNT(distinct(字段)) COUNT()函數小結 字符函數length(str)函數&#xff1a;獲取參數值的字節個數concat(str1,str2,...)函數&#xff1a;字符串拼接upper(str)、lower(str)函數:大小…

Linux高負載排查最佳實踐

在Linux系統中&#xff0c;經常會因為負載過高導致各種性能問題。那么如何進行排查&#xff0c;其實是有跡可循&#xff0c;而且模式固定。 本次就來分享一下&#xff0c;CPU占用過高、磁盤IO占用過高的排查方法。 還是那句話&#xff0c;以最佳實踐入手&#xff0c;真傳一句話…