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

国家建设部建筑业网站semir森马

国家建设部建筑业网站,semir森马,成都网站设计费用,开发项目外包在 Vue 中&#xff0c;组件之间的传值方式主要包括以下几种情况&#xff1a; 1. 父组件向子组件传值&#xff08;props&#xff09; 父组件通过 props 传递数据给子组件&#xff1a; <!-- Parent.vue --> <template><ChildComponent :msg"message"…

在 Vue 中,组件之间的传值方式主要包括以下几种情况:

1. 父组件向子组件传值(props

父组件通过 props 传递数据给子组件:

<!-- Parent.vue -->
<template><ChildComponent :msg="message" />
</template><script>
import ChildComponent from './ChildComponent.vue';
export default {components: { ChildComponent },data() {return {message: 'Hello from Parent!'};}
};
</script>
<!-- ChildComponent.vue -->
<template><p>Received message: {{ msg }}</p>
</template><script>
export default {props: {msg: String}
};
</script>

2. 子组件向父组件传值($emit

子组件通过 this.$emit 触发事件,父组件监听事件并获取值:

<!-- Parent.vue -->
<template><ChildComponent @update-message="handleMessage" /><p>Message from Child: {{ receivedMessage }}</p>
</template><script>
import ChildComponent from './ChildComponent.vue';
export default {components: { ChildComponent },data() {return {receivedMessage: ''};},methods: {handleMessage(msg) {this.receivedMessage = msg;}}
};
</script>
<!-- ChildComponent.vue -->
<template><button @click="sendMessage">Send to Parent</button>
</template><script>
export default {methods: {sendMessage() {this.$emit('update-message', 'Hello from Child!');}}
};
</script>

3. 兄弟组件传值(Event BusPinia/Vuex

兄弟组件需要一个中间桥梁,比如 Event Bus(Vue 3 不推荐)或 Pinia(推荐):

// eventBus.js (Vue 2 可用,Vue 3 推荐使用 Pinia)
import Vue from 'vue';
export const EventBus = new Vue();

BrotherA.vue 发送数据:

<template><button @click="sendMessage">Send to BrotherB</button>
</template><script>
import { EventBus } from './eventBus.js';
export default {methods: {sendMessage() {EventBus.$emit('message', 'Hello from BrotherA!');}}
};
</script>

BrotherB.vue 接收数据:

<template><p>{{ receivedMessage }}</p>
</template><script>
import { EventBus } from './eventBus.js';
export default {data() {return { receivedMessage: '' };},created() {EventBus.$on('message', msg => {this.receivedMessage = msg;});}
};
</script>

4. ref 方式(获取子组件实例)

父组件可以通过 ref 获取子组件实例并访问其方法或数据:

<!-- Parent.vue -->
<template><ChildComponent ref="childRef" /><button @click="callChildMethod">Call Child Method</button>
</template><script>
import ChildComponent from './ChildComponent.vue';
export default {components: { ChildComponent },methods: {callChildMethod() {this.$refs.childRef.childMethod();}}
};
</script>
<!-- ChildComponent.vue -->
<template><p>Child Component</p>
</template><script>
export default {methods: {childMethod() {console.log('Child method called!');}}
};
</script>

5. provideinject(适用于祖孙组件)

适用于跨层级组件通信:

<!-- GrandParent.vue -->
<template><Parent />
</template><script>
import Parent from './Parent.vue';
export default {components: { Parent },provide() {return { sharedMessage: 'Hello from GrandParent!' };}
};
</script>
<!-- Parent.vue -->
<template><Child />
</template><script>
import Child from './Child.vue';
export default {components: { Child }
};
</script>
<!-- Child.vue -->
<template><p>{{ sharedMessage }}</p>
</template><script>
export default {inject: ['sharedMessage']
};
</script>

6. Vuex 或 Pinia(全局状态管理)

适用于复杂状态管理,如 Vuex(Vue 2)或 Pinia(Vue 3):

// store.js (使用 Pinia)
import { defineStore } from 'pinia';export const useMainStore = defineStore('main', {state: () => ({message: 'Hello from Store'}),actions: {setMessage(newMsg) {this.message = newMsg;}}
});

ComponentA.vue 更新数据:

<template><button @click="updateMessage">Update Message</button>
</template><script>
import { useMainStore } from './store.js';
export default {setup() {const store = useMainStore();const updateMessage = () => store.setMessage('Updated Message!');return { updateMessage };}
};
</script>

ComponentB.vue 读取数据:

<template><p>{{ store.message }}</p>
</template><script>
import { useMainStore } from './store.js';
export default {setup() {const store = useMainStore();return { store };}
};
</script>

http://www.dtcms.com/wzjs/512467.html

相关文章:

  • 苏州高端网站建设腾讯企业邮箱
  • 简述网站建设有哪些步骤国外最好的免费建站
  • wordpress 时光网主题悟空建站seo服务
  • wordpress 网站地图插件百度站长平台网页版
  • 网站开发论文文献综述本地服务推广平台哪个好
  • 网站标题格式网站域名在哪买
  • 自己电脑做网站 外网无法访问东莞搜索排名提升
  • 哪些网站用黑体做的百度广告投放
  • 汽车建设网站发稿推广
  • 做网站的工具有哪些sem优化托管
  • 广西壮族自治区建设厅官方网站百度热搜榜怎么打开
  • 网站开发产品设计书百度指数怎么看排名
  • 网络营销与网站推广的宁波网站推广公司报价
  • 苏州高端网站建设kgu江苏关键词推广seo
  • 徐州网站定制公司哪家好免费seo优化工具
  • 自己做网站 知乎站长号
  • 哪家做网站的公司好新媒体运营哪个培训机构好
  • 南昌商城网站建设公司推广公司app主要做什么
  • 中文wordpress主题下载地址苏州seo门户网
  • 网站建设和续费电商最好卖的十大产品
  • 网站建设做得好关键词投放
  • 做网站有了空间在备案吗潍坊做网站公司
  • 娃哈哈网站建设策划书业务推广方式
  • 手机版企业网站h5韶关疫情最新消息
  • 在哪里个网站找专业做ps的人流氓网站
  • 太仓做网站的深圳百度seo优化
  • 网站建设的费用明细网络营销方式对比分析
  • wordpress电影主题网站宁德市疫情最新消息
  • 宁波建设银行网站分部百度关键词收费标准
  • 开发一个平台要多少钱百度seo优化按年收费