vue2的知识点,更多前端知识在主页,还有其他知识会持续更新
在Vue.js 2.x中,父子组件之间的通信是非常常见的情况,Vue提供了多种方法来实现这种通信。
props接收这些数据。父组件通过属性传递数据,子组件通过props接收这些数据。- <template>
- <div>
- <child-component :message="parentMessage">child-component>
- div>
- template>
-
- <script>
- import ChildComponent from './ChildComponent.vue';
-
- export default {
- components: {
- ChildComponent
- },
- data() {
- return {
- parentMessage: 'Message from parent'
- }
- }
- }
- script>
-
- <template>
- <div>
- <p>{{ message }}p>
- div>
- template>
-
- <script>
- export default {
- props: ['message']
- }
- script>
- <template>
- <div>
- <child-component @child-event="handleChildEvent">child-component>
- div>
- template>
-
- <script>
- import ChildComponent from './ChildComponent.vue';
-
- export default {
- components: {
- ChildComponent
- },
- methods: {
- handleChildEvent(message) {
- console.log('Message from child:', message);
- }
- }
- }
- script>
-
- <template>
- <div>
- <button @click="sendMessage">Send Message to Parentbutton>
- div>
- template>
-
- <script>
- export default {
- methods: {
- sendMessage() {
- this.$emit('child-event', 'Hello from child');
- }
- }
- }
- script>
- <template>
- <div>
- <child-component ref="childRef">child-component>
- <button @click="callChildMethod">Call Child Methodbutton>
- div>
- template>
-
- <script>
- import ChildComponent from './ChildComponent.vue';
-
- export default {
- components: {
- ChildComponent
- },
- methods: {
- callChildMethod() {
- this.$refs.childRef.childMethod();
- }
- }
- }
- script>
-
- <template>
- <div>
- <p>Child Componentp>
- div>
- template>
-
- <script>
- export default {
- methods: {
- childMethod() {
- console.log('Method called from parent');
- }
- }
- }
- script>

- // EventBus.js
- import Vue from 'vue';
- export const EventBus = new Vue();
-
- // ComponentA.vue
- <template>
- <div>
- <button @click="sendMessage">Send Message to Component Bbutton>
- div>
- template>
-
- <script>
- import { EventBus } from './EventBus.js';
-
- export default {
- methods: {
- sendMessage() {
- EventBus.$emit('message-from-A', 'Hello from Component A');
- }
- }
- }
- script>
-
- // ComponentB.vue
- <template>
- <div>
- <p>{{ message }}p>
- div>
- template>
-
- <script>
- import { EventBus } from './EventBus.js';
-
- export default {
- data() {
- return {
- message: ''
- };
- },
- created() {
- EventBus.$on('message-from-A', (message) => {
- this.message = message;
- });
- }
- }
- script>
- // store.js
- import Vue from 'vue';
- import Vuex from 'vuex';
-
- Vue.use(Vuex);
-
- export default new Vuex.Store({
- state: {
- message: ''
- },
- mutations: {
- setMessage(state, payload) {
- state.message = payload;
- }
- }
- });
-
- // ComponentA.vue
- <template>
- <div>
- <button @click="sendMessage">Send Message to Component Bbutton>
- div>
- template>
-
- <script>
- export default {
- methods: {
- sendMessage() {
- this.$store.commit('setMessage', 'Hello from Component A');
- }
- }
- }
- script>
-
- // ComponentB.vue
- <template>
- <div>
- <p>{{ message }}p>
- div>
- template>
-
- <script>
- export default {
- computed: {
- message() {
- return this.$store.state.message;
- }
- }
- }
- script>