当前位置: 首页 > news >正文

网站加手机建设png图标WordPress 打赏插件

网站加手机建设png图标,WordPress 打赏插件,石家庄有什么做网站的公司,计算机类哪个专业最吃香在 Vue 3 中,组件间传值有多种方式,以下是几种常见的方式 父组件向子组件传值(通过 props):以下是几个父组件向子组件传值的示例:示例 1:传递字符串示例 2:传递数字示例 3&#xff1…

在 Vue 3 中,组件间传值有多种方式,以下是几种常见的方式

    • 父组件向子组件传值(通过 props):
      • 以下是几个父组件向子组件传值的示例:
        • 示例 1:传递字符串
        • 示例 2:传递数字
        • 示例 3:传递对象
        • 示例 4:传递数组
        • 示例 5:传递布尔值
    • 子组件向父组件传值(通过自定义事件):
      • 子组件使用 v-model 向父组件传值的示例代码:
    • 通过 provide 和 inject:
    • 在父组件和子组件中使用:

父组件向子组件传值(通过 props):

<!-- 父组件 -->
<template><ChildComponent :message="parentMessage" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
const parentMessage = '这是来自父组件的值';
</script><!-- 子组件 -->
<template><div>{{ message }}</div>
</template><script setup>
defineProps(['message']);
</script>

以下是几个父组件向子组件传值的示例:

示例 1:传递字符串

父组件:

<template><ChildComponent :message="messageFromParent" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
const messageFromParent = '这是来自父组件的消息';
</script>

子组件:

<template><div>{{ message }}</div>
</template><script setup>
defineProps(['message']);
</script>
示例 2:传递数字

父组件:

<template><ChildComponent :count="5" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div>接收到的数字: {{ count }}</div>
</template><script setup>
defineProps(['count']);
</script>
示例 3:传递对象

父组件:

<template><ChildComponent :userInfo="{ name: '张三', age: 25 }" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div>用户信息: {{ userInfo.name }}, {{ userInfo.age }}</div>
</template><script setup>
defineProps(['userInfo']);
</script>
示例 4:传递数组

父组件:

<template><ChildComponent :fruits="['苹果', '香蕉', '橙子']" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div>水果列表: <ul><li v-for="fruit in fruits" :key="fruit">{{ fruit }}</li></ul></div>
</template><script setup>
defineProps(['fruits']);
</script>
示例 5:传递布尔值

父组件:

<template><ChildComponent :isActive="true" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div v-if="isActive">当前状态为活跃</div><div v-else>当前状态为不活跃</div>
</template><script setup>
defineProps(['isActive']);
</script>

子组件向父组件传值(通过自定义事件):

<!-- 父组件 -->
<template><ChildComponent @childEvent="handleChildEvent" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';function handleChildEvent(valueFromChild) {console.log('从子组件接收到的值:', valueFromChild);
}
</script><!-- 子组件 -->
<template><button @click="emitEvent">向父组件传值</button>
</template><script setup>
defineEmits(['childEvent']);function emitEvent() {const valueToSend = '这是来自子组件的值';emit('childEvent', valueToSend);
}
</script>

子组件使用 v-model 向父组件传值的示例代码:

父组件:
html
复制

父组件接收到的值: {{ inputValue }}

子组件:
html
复制

<input type=“text” :value=“value” @input=“$emit(‘update:value’, $event.target.value)” />

在上述示例中,父组件通过 v-model:value=“inputValue” 将 inputValue 与子组件进行绑定。子组件中的输入框的值通过 :value=“value” 与父组件传来的值进行关联,当输入框的值发生变化时,通过 @input=“$emit(‘update:value’, $event.target.value)” 触发 update:value 事件并将新的值传递给父组件,从而实现子组件向父组件传值。

通过 provide 和 inject:

<!-- 父组件 -->
<template><ChildComponent />
</template><script setup>
import { provide } from 'vue';provide('sharedValue', '这是共享的值');
</script><!-- 子组件 -->
<template><div>{{ sharedValue }}</div>
</template><script setup>
import { inject } from 'vue';const sharedValue = inject('sharedValue');
</script>

使用 Vuex 状态管理:
首先安装 vuex :npm install vuex@next --save
创建一个 store.js 文件:

import { createStore } from 'vuex';export default createStore({state: {commonValue: '这是全局的值',},mutations: {updateCommonValue(state, newValue) {state.commonValue = newValue;}},actions: {},getters: {}
});

在父组件和子组件中使用:

<!-- 父组件或子组件 -->
<template><div>{{ $store.state.commonValue }}</div>
</template><script setup>
import { useStore } from 'vuex';const store = useStore();function updateValue() {store.commit('updateCommonValue', '新的值');
}
</script>

这些是 Vue 3 中常见的组件间传值方式,您可以根据具体的项目需求选择合适的方法。

http://www.yayakq.cn/news/982321/

相关文章:

  • 循化网站建设公司wordpress 图片显示插件下载
  • 兰州市政建设集团官网用seo对网站做分析
  • 礼县建设局网站建设维护网站运营方案
  • 网站seo优化书籍免费做电子目录的网站
  • 来宾绍兴seo网站托管方案直播类型网站开发
  • 晋江在线网站建设wordpress恶意注册
  • 什么是网站原创文章个人网站转企业
  • 做网站公司介绍ppt做网站维护的是什么公司
  • 做个网站多钱汉化版wordpress
  • wordpress 优秀网站郑州网站哪家好
  • 开个捕鱼网站怎么做网站的建设成本
  • 青岛市城市建设管理局网站wordpress 新文章订阅
  • 怎么修改网站上传附件大小校园门户网站建设公司
  • 花都网站设计都佛山网站建设公司如何组建
  • 六安市网站制作公司wordpress 标签禁用
  • 大气预警在建设局网站网站开发和前端开发
  • 珠海市手机网站建设公司滁州建设网站
  • 淘宝网站建设单子好接吗威海网站开发公司电话
  • 广州做手机网站建设计算机程序网站开发是什么
  • 直播做ppt的网站有哪些想在百度做网站
  • 信宜手机网站建设公司网站免备案空间
  • 无锡网站建设楚天软件门户 网站 asp
  • 网上商城建站工作室it运维有前途吗
  • wordpress store昆山优化外包
  • 商务网站开发南昌商城建设
  • 做第一个php网站动力网页设计制作公司
  • 广州市门户网站建设品牌电商指的是什么行业
  • 杭州网站优化搜索wordpress 分类排序
  • 外贸网站搭建难不难外网网站
  • 专门做2k名单的网站ui图标素材网站