Vue3中setup函数如何进行组间通讯

简介: Vue3中setup函数如何进行组间通讯

父子组件通讯

父传子:

父组件代码示例:

<template>
  <children :list="list"></children>
  <div>
    <input v-model="value" type="text" placeholder="请输入" />
    <button @click="handleAdd"> 添加 </button>
  </div>
</template>
<script setup>
  import { ref } from 'vue'
  import Children from '@/views/parentChildCommunicat/parentToChildren/components/children.vue'
  const list = ref(['JavaScript', 'HTML', 'CSS'])
  const value = ref('')
  const handleAdd = () => {
    list.value.push(value.value)
    value.value = ''
  }
</script>

子组件代码:

<template>
  <ul>
    <li v-for="item in props.list" :key="item">{{ item }}</li>
  </ul>
</template>
<script setup>
  import { defineProps } from 'vue'
  const props = defineProps({
    list: {
      type: Array,
      default: () => []
    }
  })
</script>

子传父:

子组件:

<template>
  <div>
    <input v-model="value" type="text" placeholder="请输入" />
    <button @click="handleAdd"> 添加 </button>
  </div>
</template>
<script setup>
  import { ref, defineEmits } from 'vue'
  const value = ref('')
  const emits = defineEmits(['add'])
  const handleAdd = () => {
    emits('add', value.value)
    value.value = ''
  }
</script>

父组件

<template>
  <ul>
    <li v-for="item in list" :key="item">{{ item }}</li>
  </ul>
  <son @add="add" />
</template>
<script setup>
  import { ref } from 'vue'
  import Son from './components/son.vue'
  const list = ref(['JavaScript', 'HTML', 'CSS'])
  const add = (value) => {
    list.value.push(value)
  }
</script>

v-model 实现父子组件双向通信

子组件:

<template>
  <div>
    <input v-model="value" type="text" placeholder="请输入" />
    <button @click="handleAdd"> 添加 </button>
  </div>
</template>
<script setup>
  import { ref, defineEmits, defineProps } from 'vue'
  const value = ref('')
  const props = defineProps({
    list: {
      type: Array,
      default: () => []
    }
  })
  const emits = defineEmits(['update:list'])
  const handleAdd = () => {
    const arr = props.list
    arr.push(value.value)
    emits('update:list', arr)
    value.value = ''
  }
</script>

父组件

<template>
  <ul>
    <li v-for="i in list" :key="i">{{ i }}</li>
  </ul>
  <child-components v-model:list="list" />
</template>
<script setup>
  import { ref } from 'vue'
  import ChildComponents from './components/child-components.vue'
  const list = ref(['JavaScript', 'HTML', 'CSS'])
</script>

跨组件通信

Vuex和Pinia


相关文章
|
2天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable为了适配文件上传改造VForm3的代码记录
ruoyi-nbcio-plus基于vue3的flowable为了适配文件上传改造VForm3的代码记录
11 1
|
2天前
|
JavaScript 前端开发
vue2升级到vue3的一些使用注意事项记录(四)
vue2升级到vue3的一些使用注意事项记录(四)
|
2天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable收回任务后重新进行提交表单的处理
ruoyi-nbcio-plus基于vue3的flowable收回任务后重新进行提交表单的处理
|
2天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable多租户机制
ruoyi-nbcio-plus基于vue3的flowable多租户机制
|
2天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable的消息中心我的消息的升级修改
ruoyi-nbcio-plus基于vue3的flowable的消息中心我的消息的升级修改
|
3天前
|
JavaScript
VUE里的find与filter使用与区别
VUE里的find与filter使用与区别
12 0
|
3天前
|
JavaScript
vue页面加载时同时请求两个接口
vue页面加载时同时请求两个接口
|
3天前
|
JavaScript
vue里样式不起作用的方法,可以通过deep穿透的方式
vue里样式不起作用的方法,可以通过deep穿透的方式
12 0
|
3天前
|
移动开发 JavaScript 应用服务中间件
vue打包部署问题
Vue项目`vue.config.js`中,`publicPath`设定为&quot;/h5/party/pc/&quot;,在线环境基于打包后的`dist`目录,而非Linux的`/root`。Nginx代理配置位于`/usr/local/nginx/nginx-1.13.7/conf`,包含两个相关配置图。
vue打包部署问题
|
3天前
|
JavaScript 前端开发
iconfont 图标在vue里的使用
iconfont 图标在vue里的使用
15 0