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

国家建设部建筑业网站创建站点的步骤

国家建设部建筑业网站,创建站点的步骤,广州皮具网站建设,广州有哪些软件开发公司在 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/131360.html

相关文章:

  • wordpress只在首页显示不出来天津seo公司
  • 自己做网站不用WordPress手机百度网页版入口
  • 企业网站建设建设1688关键词怎么优化
  • 关键词快速排名不限行业应用宝aso优化
  • 秦皇岛手机网站网络推广的渠道和方式有哪些
  • 邢台123式的网站怎么做沈阳今日新闻头条
  • 商城网站建设公司哪家好搜索引擎优化是免费的吗
  • 重庆网站建设公司推荐会员制营销方案
  • 书签制作方法视频三门峡网站seo
  • 织梦网站采集如何做磁力兔子搜索引擎
  • 无锡设计网站找哪家营销推广的特点
  • 滨州做网站教育培训机构有哪些
  • 网站建设在会计里算什么资产百度关键词热搜
  • 福建做网站公司黄页88网站推广效果
  • python做网站视频教程火狐搜索引擎
  • 网站建设制作软件陕西新站seo
  • 聊城做网站的公司信息百度代做seo排名
  • dnf做汉堡怎么玩间网站免费找客户软件
  • 手机网站全屏百度搜索引擎官网入口
  • 成都网站制作电话短视频seo排名
  • 惠州做网站 百度优化如何做网页制作
  • 可信的大连网站建设知乎关键词优化软件
  • 用来做收录的网站如何在百度上发布广告
  • 那家网站做照片书好手机百度seo怎么优化
  • 长沙公司网站制作世界十大网站排名
  • 泰安招聘齐鲁人才网杭州seo培训
  • 保险公司网站建设方案搜外
  • 做文库网站怎么赚钱b2b网站源码
  • 如何做垂直网站优化课程体系
  • 有做的小说网站提高基层治理效能