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

网站建设综合实训ppt海南网站建设

网站建设综合实训ppt,海南网站建设,动态网站建设实训baogao,专门做民宿的网站全局事件总线(GlobalEventBus) 一种可以在任意组件通信的方式,本质上就是一个对象,它必须满足以下条件 所有的组件对象都必须能看见他这个对象必须能够使用$ on $ emit $ off方法取绑定、触发和解绑事件 使用步骤 定义全局事件总线 //创建VUE new V…

全局事件总线(GlobalEventBus)

一种可以在任意组件通信的方式,本质上就是一个对象,它必须满足以下条件

  1. 所有的组件对象都必须能看见他
  2. 这个对象必须能够使用$ on $ emit $ off方法取绑定、触发和解绑事件

使用步骤

  1. 定义全局事件总线
//创建VUE
new Vue({el:'#app',render:h=> h(App),beforeCreate(){Vue.prototype.$bus=this //安装全局事件总线},})
  1. 使用事件总线
    a.接收数据:A组件想接收数据,则在A组件中给$bus 绑定自定义事件,事件的回调留在A组件自身
export default{methods(){demo(data){...}}...mounted(){this.$bus.$on('xxx',this.demo)}
}

b.提供数据:this.$bus.$emit(''xxx,data)
3. 最好在beforeDestroy钩子中,用**$off()**去解绑当前组件所用到的事件。

src/main.js

//引入vue
import Vue from 'vue'
//引入APP.vue
import App from './App.vue'//关闭Vue的生产提示
Vue.config.productionTip=false//创建VUE
new Vue({el:'#app',render:h=> h(App),beforeCreate(){Vue.prototype.$bus=this //安装全局事件总线},})

src/App.vue

<template><div class="app"><h1>{{msg}}</h1><School></School><Student></Student></div>
</template><script>import Student from './components/Student'import School from './components/School'export default {name:'App',components:{School,Student},data() {return {msg:'你好啊!'}}}
</script><style scoped>.app{background-color: gray;padding: 5px;}
</style>

src/compoents/School.vue

<template><div class="school"><h2>学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template><script>export default {name:'School',data() {return {name:'尚硅谷',address:'北京',}},mounted() {// console.log('School',this)this.$bus.$on('hello',(data)=>{console.log('我是School组件,收到了数据',data)})},beforeDestroy() {this.$bus.$off('hello')},}
</script><style scoped>.school{background-color: skyblue;padding: 5px;}
</style>

src/compoents/Student.vue

<template><div class="student"><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><button @click="sendStudentName">把学生名给School组件</button></div>
</template><script>export default {name:'Student',data() {return {name:'张三',sex:'男'}},mounted() {// console.log('Student',this.x)},methods: {sendStudentName(){this.$bus.$emit('hello',this.name)}},}
</script><style  scoped>.student{background-color: pink;padding: 5px;margin-top: 30px;}
</style>

在这里插入图片描述

http://www.hkea.cn/news/13081/

相关文章:

  • 第三方做公司网站推广平台网站热狗网
  • 网站制作图片插入代码关键词优化靠谱推荐
  • 四川万景建设工程有限公司网站天眼查企业查询
  • 网站建设 长摊 无形资产必应bing搜索引擎
  • 西安域名注册网站建设柏乡seo快排优化
  • 有没有做奥数题的网站班级优化大师官网登录
  • 合肥专业做网站的百度站长社区
  • 网站源码 正在建设中app推广30元一单平台
  • 法律顾问 网站 源码关键词挖掘ppt
  • 广东哪家网站建设哪家公司好百度小说搜索风云排行榜
  • app软件怎么开发优化工具箱下载
  • 网站做竞价需要什么信息业务推广方式有哪些
  • wordpress网站和微信公众号爱站关键词查询
  • 把网站制作成app网页设计模板素材图片
  • 坪山区住房和建设局网站五种营销工具
  • 动漫网站开发需求分析线上推广平台都有哪些
  • 安徽优化开发区福州搜索引擎优化公司
  • 个性flash网站1+x网店运营推广
  • 网站上怎么做动画广告网站推广教程
  • 做医疗的网站html网页制作模板
  • wordpress站点登陆重庆搜索引擎seo
  • 天津市企业网站设计公司二十条优化措施
  • 邯郸网站建设渠道网站网页设计
  • 深圳网站建设61916新冠疫情最新数据
  • 做红包网站西点培训班一般要多少学费
  • 免费公司主页网站搜索引擎是软件还是网站
  • 上海企业网站模板建站网络营销管理系统
  • 做网站要买多大服务器百度收录入口在哪里
  • 特色的南昌网站制作南宁网络推广热线
  • wordpress主题一键生成大型seo公司