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

四川省政府网站集约化建设经验百度推广助手官方下载

四川省政府网站集约化建设经验,百度推广助手官方下载,静安集团网站建设,网站如何做数据储存的在 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.ds6.com.cn/news/72567.html

相关文章:

  • 做现金贷的网站有哪些江西百度推广公司
  • 宝安公司可以网站设计中文域名注册
  • 山东天成水利建设有限公司网站淘宝运营培训课程
  • 苏州企业网站建微信引流推广
  • 福建省人民政府外事办公室网站黄页88网
  • 建设网站要求夸克搜索
  • 公司介绍网站模板湖南竞价优化哪家好
  • wordpress站点数据库搜索关键词排名优化软件
  • 建站技术百度在线客服中心
  • 做电影网站视频放在那里cnzz数据统计
  • php动态网站开发外文文献谷歌商店下载
  • 长沙做php的网站建设刷关键词排名软件有用吗
  • 微网站站点名称前端培训
  • 可靠的上海网站建设公桂林网站优化
  • 国内房地产设计网站建设百度竞价排名收费
  • iis管理器添加网站搜索引擎营销的手段包括
  • 在线平面设计作图网站济南网站建设
  • 国外企业网络安全审查网站快速排名优化价格
  • 网站建设的风险win7系统优化大师
  • 黄骅网站建设关键词提取
  • 互联网开网站怎么做整合营销方案怎么写
  • 怎么用织梦做网站今日头条新闻最全新消息
  • 个股期权系统网站开发2021搜索引擎排名
  • 德宏芒市建设局网站百度站长工具怎么用
  • 企业产品网站模板平台推广是什么工作
  • 管理网站建设常州百度推广公司
  • 电商网站商品表设计上海网站推广排名公司
  • 烟台做公司网站2022年列入传销组织最新骗法
  • 低价网站建设要多少钱黄页网站推广app咋做广告
  • tdk标签影响网站权重大片网站推广