• Vue3的组件如何通讯


    一、defineProps,defineEmits

    子组件nameChange.vue

    1. <template>
    2. <div class="title">
    3. 姓:{{ firstName }}
    4. </div>
    5. <div>
    6. 名:{{ lastName }}
    7. </div>
    8. {{ name }}
    9. <button @click="clickTap">传参</button>
    10. </template>
    11. <script setup lang="ts">
    12. import { Ref } from 'vue';
    13. type props = {
    14. firstName: string,
    15. lastName: string
    16. }
    17. const prop = withDefaults(defineProps<props>(), {
    18. firstName: 'c',
    19. lastName: 'qs'
    20. })
    21. const emit = defineEmits<{
    22. (e: "on-click", name: props): void
    23. }>()
    24. const clickTap = () => {
    25. emit('on-click', prop);
    26. }
    27. </script>
    28. <style>
    29. </style>

    父组件

    1. <template>
    2. <nameChange @on-click="getName" first-name="c" last-name="qs"></nameChange>
    3. </template>
    4. <script setup lang="ts">
    5. import { reactive } from 'vue';
    6. type props = {
    7. firstName: string,
    8. lastName: string
    9. }
    10. let names = reactive<props>({
    11. firstName: '1',
    12. lastName: '2'
    13. });
    14. import nameChange from "./components/nameChange.vue";
    15. const getName = (data: props) => {
    16. names.firstName = data.firstName;
    17. names.lastName = data.lastName;
    18. }
    19. </script>
    二、provide,inject
    1. //注入依赖
    2. const colorName = ref<string>('#fff449')
    3. provide('colorName', colorName)
    4. //获取依赖
    5. const color = inject<Ref<string>>('colorName');
    6. color!.value = 'blue';
    三、defineExpose

    子组件暴露数据

    1. <script setup lang="ts">
    2. defineExpose({
    3. name: 'cqs'
    4. })
    5. </script>

    父组件通过ref获取

    1. <template>
    2. <nameChange ref="refName"></nameChange>
    3. </template>
    4. <script setup lang="ts">
    5. import nameChange from "./components/nameChange.vue";
    6. const refName = ref<InstanceType<typeof nameChange>>()
    7. onMounted(() => {
    8. console.log(refName.value);
    9. })
    10. </script>
    四、mitt

    通过插件mitt,相当于vue2的bus总线

    注册

    1. import { createApp } from 'vue'
    2. import App from './App.vue'
    3. import mitt from 'mitt'
    4. const Mitt = mitt();
    5. const app = createApp(App)
    6. app.config.globalProperties.$Bus = Mitt;
    7. app.mount('#app')

    监听

    1. <template>
    2. </template>
    3. <script setup lang="ts">
    4. import { ref, getCurrentInstance } from 'vue';
    5. type prop1 = {
    6. name: string
    7. }
    8. const title = ref('这是A组件')
    9. const instance = getCurrentInstance()
    10. instance?.proxy?.$Bus.on('changeName', (name:any) => {
    11. title.value = name
    12. });
    13. </script>

    触发

    1. <template>
    2. <button @click="changeName">修改name</button>
    3. </template>
    4. <script lang="ts" setup>
    5. import { ref } from 'vue'
    6. import { getCurrentInstance } from 'vue';
    7. const title = ref('这是B组件')
    8. const instance = getCurrentInstance()
    9. const changeName = () => {
    10. instance?.proxy?.$Bus.emit('changeName', '我是新数据')
    11. }
    12. </script>
    五、v-model

    父组件,可以添加v-model:textVal1.isSb自定义修饰符,通过textVal1Modifiers获取

    1. <template>
    2. isShow:{{ value }}
    3. text: {{ text }}
    4. <dialcoag v-model:textVal1.isSb="text" v-model="value"></dialcoag>
    5. </template>
    6. <script setup lang="ts">
    7. import dialcoag from './components/dialoag.vue';
    8. import { ref } from 'vue';
    9. const value = ref<boolean>(true)
    10. const text = ref<string>('cqs')
    11. </script>
    12. <style>
    13. * {
    14. text-align: left;
    15. }
    16. </style>

    子组件

    1. <template>
    2. <div>{{ modelValue }}</div>
    3. <div>{{ textVal1 }}</div>
    4. <button @click="close">关闭</button>
    5. </template>
    6. <script setup lang="ts">
    7. const props = defineProps<{
    8. modelValue: boolean,
    9. textVal1: string,
    10. textVal1Modifiers?: {
    11. isSb: boolean
    12. }
    13. }>()
    14. const emit = defineEmits(['update:modelValue', 'update:textVal1'])
    15. const close = () => {
    16. emit('update:modelValue', false);
    17. emit('update:textVal1', props.textVal1Modifiers?.isSb ? 'dadadada' + 'sb' : '123456 ')
    18. }
    19. </script>

     

  • 相关阅读:
    vs2017 编译Qt 5.11.2 源码
    2022年全网最全最细最流行的自动化测试工具有哪些?
    PCA和SVD数据降维
    【雨夜】业务中 自定义异常用 Exception 还是 RuntimeException? 为什么?
    App上架Apple App Store和Google Play流程
    《从零开始大模型开发与微调 :基于PyTorch与ChatGLM》简介
    spark 读操作
    正则表达式“~= s,/,\\,g“作用
    操作指南|JumpServer用户权限体系的使用实践
    无人机地面站技术,无人机地面站理论基础详解
  • 原文地址:https://blog.csdn.net/QAEARQ/article/details/134517922