vue使用组件和简单的组件通信

注册和使用组件

在vue2中创建组件,并且使用它。

首先在main.js中创建vue实例:

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

创建helloworld组件,即创建hellworld.vue文件:
用export default暴露,以被调用这个组件。

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      msg: "Welcome to Your Vue.js App",
    };
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1 {
  font-weight: normal;
}
a {
  color: #42b983;
}
</style>

在app.vue中注册并引用组件hello:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <hello></hello>
  </div>
</template>

<script>
import hello from "./components/HelloWorld"
export default {
  name: 'App',
  components: {
    hello,
  },
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

新建命令窗口:npm run dev (有的是 npm run serve)

在这里插入图片描述

打开连接http://localhost:8080:

在这里插入图片描述

组件间的通信

创建一个Bus.js文件暴露实例。用以承载通信:

import Vue from 'vue';

const Bus = new Vue();

export default Bus;


在发出信息的组件中设置事件:

事件写在methods中。

Bus.$emit(“data-aside2”, this.year);第一个参数是事件名字,第二个是要传递的信息。

<template>
  <div>
    <el-menu>
      <el-submenu
        v-for="(item, index) in items"
        :key="index"
        :index="index + ''"
      >
        <template slot="title"
          ><i class="el-icon-user"></i>{{ item.grade }}</template
        >
        <el-menu-item
          :index="index + '0.4'"
          @click="setgrade(item.grade), send0(), send1()"
          ><i class="el-icon-s-custom"></i>社团负责人</el-menu-item
        >
        <el-menu-item
          :index="index + '0.6'"
          @click="setgrade(item.grade), send0(), send2()"
          ><i class="el-icon-s-custom"></i>社团成员</el-menu-item
        >
      </el-submenu>
    </el-menu>
  </div>
</template>


<script>
import Bus from "../../../assets/js/Bus";
export default {
  name: "Asideedge",
  data() {
    return {
      clickState: true,
    };
  },
  methods: {
    send0() {
      Bus.$emit("data-aside0", this.clickState);
    },
    send1() {
      Bus.$emit("data-aside1", this.year);
      console.log(this.year);
    },
    send2() {
      Bus.$emit("data-aside2", this.year);
      console.log(this.year);
    },
  },
};
</script>

在接收信息的组件中设置监听事件。
Bus.$on(“data-aside2”, (msg) => {
this.msg = msg;
});
第一个参数被监听的那个事件的名字,第二个用处理接收的信息。

<template>
  <div class="home">
       <Mainbody v-show="state"></Mainbody>
       <Emptybody v-show="!state"></Emptybody>
  </div>
</template>

<script>
import Bus from "../../assets/js/Bus";
import Mainbody from "./componnents/mainbody.vue";
import Emptybody from "./componnents/emptybody.vue";

export default {
  name: "Home",
  components: {
    Mainbody,
    Emptybody,
  },
  data() {
    return {
      msg:"成员信息",
      state: false,
    };
  },
  mounted() {
    Bus.$on("data-aside0", (state) => {
      this.state = state;
    });
       Bus.$on("data-aside1", (msg) => {
      this.msg = msg; 
    });
    Bus.$on("data-aside2", (msg) => {
      this.msg = msg;
    });
  },
};
</script>

这样被监听的事件和监听它的事件都设置完毕就可以通信了。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Vue组件和父组件通信可以通过props和emit来实现,而子组件和兄弟组件通信可以通过父组件作为中介来实现。 1. 使用props和emit实现子组件和父组件通信: 父组件通过prop向子组件传递数据,子组件通过emit触发事件并传递数据给父组件。 父组件: ``` <template> <div> <child-component :message="msg" @update="updateMsg"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { msg: 'Hello World' } }, methods: { updateMsg(newMsg) { this.msg = newMsg } } } </script> ``` 子组件: ``` <template> <div> <p>{{ message }}</p> <button @click="updateMessage">Update Message</button> </div> </template> <script> export default { props: ['message'], methods: { updateMessage() { this.$emit('update', 'New Message') } } } </script> ``` 2. 使用事件总线实现子组件和兄弟组件通信: 父组件作为事件总线,子组件通过$emit触发事件并传递数据,兄弟组件通过$on监听事件并接收数据。 父组件: ``` <template> <div> <child-component></child-component> <sibling-component></sibling-component> </div> </template> <script> import Vue from 'vue' import ChildComponent from './ChildComponent.vue' import SiblingComponent from './SiblingComponent.vue' export default { components: { ChildComponent, SiblingComponent } } // 创建事件总线 export const eventBus = new Vue() </script> ``` 子组件: ``` <template> <div> <button @click="updateMessage">Update Message</button> </div> </template> <script> import { eventBus } from './ParentComponent.vue' export default { methods: { updateMessage() { eventBus.$emit('update-message', 'New Message') } } } </script> ``` 兄弟组件: ``` <template> <div> <p>{{ message }}</p> </div> </template> <script> import { eventBus } from './ParentComponent.vue' export default { data() { return { message: '' } }, mounted() { eventBus.$on('update-message', (newMsg) => { this.message = newMsg }) } } </script> ``` 以上是两种常见的子组件和其他组件通信的方式,根据实际情况选择合适的方式来实现即可。 ### 回答2: Vue组件和父组件之间的通信可以通过以下几种方式实现。 1. Props和$emit:父组件通过props将数据传递给子组件,在子组件中可以直接使用。子组件想要将数据传递给父组件时,可以通过使用$emit方法触发父组件监听的事件,并将数据传递给父组件。 2. 子组件和父组件共享数据:当父组件和子组件需要共享数据时,可以在父组件中定义一个data对象,并将该对象传递给子组件。子组件可以通过props接收父组件传递的数据,并直接修改该数据,从而实现数据共享和双向绑定。 3. 使用ref获取子组件实例:在父组件中可以通过使用ref属性获取子组件的实例,从而直接调用子组件中的方法或访问子组件的属性。 4. 使用事件总线:可以使用一个全局的事件总线或创建一个专属于父子组件的事件总线来实现组件间的通信。父子组件通过触发和监听事件来传递数据。 5. 使用vuex进行状态管理:当应用较为复杂时,可以使用vuex来进行状态管理,父子组件可以通过vuex获取和修改共享的状态数据。 总之,Vue提供了多种方式实现父子组件之间的通信,采用合适的方式可以更好地满足应用的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lazy_Goat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值