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

网站建设服务费的税率怎样创建网页

网站建设服务费的税率,怎样创建网页,东莞58同城网招聘,网站建设术语解释Vue3中采用EventBus方式进行组件间通信与Vue2有一定区别 1.创建EventBus 在Vue2中,我们可以在main.js中创建一个全局的EventBus,代码如下: // EventBus.js import Vue from vue const EventBus new Vue() export default EventBus// main.…

Vue3中采用EventBus方式进行组件间通信与Vue2有一定区别

1.创建EventBus

在Vue2中,我们可以在main.js中创建一个全局的EventBus,代码如下:

// EventBus.js
import Vue from 'vue'
const EventBus = new Vue()
export default EventBus// main.js
import EventBus from './EventBus'
Vue.prototype.$bus = EventBus

在Vue3中,我们需要使用createApp来创建Vue实例,并使用provide和inject来创建全局的EventBus,代码如下:

// EventBus.js
import { createApp } from 'vue'
const app = createApp({})
const EventBus = app.provide('EventBus', new app())
export default EventBus// main.js
import EventBus from './EventBus'
createApp(App).use(EventBus).mount('#app')-----------------
// 在 main.js 或入口文件中
import { createApp, provide } from 'vue';
import App from './App.vue';const app = createApp(App);
const eventBus = createApp({});// 将 eventBus 实例作为提供者提供给子组件
app.provide('eventBus', eventBus);app.mount('#app');

2.使用EventBus进行通信

在Vue2中,我们可以通过$emit和$on方法来进行通信,代码如下:

// 发送通信
this.$emit('event', data)// 接收通信
this.$on('event', (data) => {console.log(data)
})// 销毁
this.$off('event')

在Vue3中,我们可以通过$emit和$on方法来进行通信,但是需要在组件中使用AppContext来获取EventBus,代码如下:

// 发送通信
const app = getCurrentInstance().appContext.app
app.config.globalProperties.$EventBus.emit('event', data)// 接收通信
const app = getCurrentInstance().appContext.app
app.config.globalProperties.$EventBus.on('event', (data) => {console.log(data)
})//  销毁
const app = getCurrentInstance().appContext.app
app.config.globalProperties.$EventBus.off('event')-------------------
import { inject } from 'vue';export default {// ...created() {const eventBus = inject('eventBus');// 使用 eventBus 进行事件的发布和订阅},// ...
}

总的来说,Vue3与Vue2在EventBus方式上的区别不大,只是在创建全局EventBus的方式上有所不同,但是使用起来差异较大,需要根据实际情况进行选择。


封装EventBus.js

类方式 

class Event {constructor() {this.queue = {};this.onceQueue = {};}$on(name, callback) {this.queue[name] = this.queue[name] || [];this.queue[name].push(callback);}$once(name, callback) {this.onceQueue[name] = this.onceQueue[name] || [];this.onceQueue[name].push(callback);}$off(name, callback) {if (callback) {if (this.queue[name]) {for (var i = 0; i < this.queue[name].length; i++) {if (this.queue[name][i] === callback) {this.queue[name].splice(i, 1);break;}}}} else {delete this.queue[name];}}$emit(name, data) {if (this.queue[name]) {this.queue[name].forEach(function (callback) {callback(data);});}if (this.onceQueue[name]) {this.onceQueue[name].forEach(function (callback) {callback(data);});delete this.onceQueue[name];}}
}export default new Event();

使用 

import Bus from '@/utils/EventBus';
Bus.$on('test', (data) => {})
Bus.$emit('close')
beforeUnmount() {
    Bus.$off('test', fun)
}

构造函数方式

function E() { }// 函数E的原型对象
E.prototype = {// on方法:接受订阅名,订阅函数,上下文对象on: function (name, callback, context) {// 初始化e仓库var e = this.e || (this.e = {})// 收集订阅函数// 包装为对象,收集订阅函数与上下文对象; (e[name] || (e[name] = [])).push({fn: callback,context})// 返回实例对象return this},// once函数:接收订阅名,订阅函数,上下文对象// 与on的区别是:once函数收集只执行一遍的订阅函数once: function (name, callback, context) {let self = this// 包装对象,用于自定义执行逻辑(删除操作)function listener() {self.off(name, listener)callback.apply(context, arguments)}// 保存原始函数listener._ = callback// 使用on收集自定义后的函数// 执行on方法会返回this,所以once函数内不需要返回thisreturn this.on(name, listener, context)},// emit方法用于触发订阅函数:接收订阅名称emit: function (name) {// 收集参数let args = [].slice.call(arguments, 1)// 收集订阅函数数组let events = ((this.e || (this.e = {}))[name] || []).slice()let i = 0let len = events.length// 循环执行订阅函数for (; i < len; i++) {// 使用apply调用函数并绑定thisevents[i].fn.apply(events[i].context, args)}// 返回this实例return this},// off用于删除订阅函数:接收订阅名和订阅函数off: function (name, callback) {let e = this.e || (this.e = {})// 获取订阅名称对应的数组let events = e[name]let liveEvents = []// 处理函数数组&传入的订阅函数是否都存在?if (events && callback) {// 循环遍历,过滤操作for (let i = 0, len = events.length; i < len; i++) {// 判断数组中的订阅函数是否与传入的订阅函数相等?// 使用once创建的函数取_属性中的原始函数进行对比if (events[i].fn !== callback && events[i].fn._ !== callback) {liveEvents.push(events[i])}}}// 重置订阅名结果数组(liveEvents.length) ? e[name] = liveEvents : delete e[name]// 返回实例thisreturn this}
}export default {$on: (...args) => E.prototype.on(...args),$once: (...args) => E.prototype.once(...args),$off: (...args) => E.prototype.off(...args),$emit: (...args) => E.prototype.emit(...args)
}

使用 

import Bus from "@/utils/EventBus2";
Bus.$on('test', (data) => {})
Bus.$emit('close')
beforeUnmount() {
    Bus.$off('test', fun)
}

http://www.dinnco.com/news/10000.html

相关文章:

  • 哪家企业网站做的好济南网站建设公司选济南网络
  • 动态网站开发 课程说明所有的竞价托管公司
  • 网站架构 规划手机如何制作网站
  • 天津网站建设交易网络营销策划的流程
  • 没有服务器做网站seo网站推广教程
  • 做网站找不到客户专业seo优化推广
  • 惠州网站建设设计如何推广网站
  • ps做网站的视频网络市场调研的方法
  • 无锡网站建设公司怎么样怎么样引流加微信
  • 做企业平台的网站有哪些内容免费制作网站的软件
  • 让网站不要保存密码怎么做厦门seo小谢
  • 苏州网站开发网站建立费用抖音流量推广神器软件
  • 微信网站制作价格推广策划
  • 蓝领网站做的最好上海搜索引擎优化1
  • 安徽网站关键字优化外贸google推广
  • 蚌埠网站建设费用草根站长工具
  • 网站建设与网页设计课程设计优化大师优化项目有哪些
  • 营销型网站建设拓客公司联系方式
  • 做听书网站怎么做windows优化大师自动安装
  • 建设一个网站需要什么360优化大师
  • 佛山高端外贸网站建设河南seo和网络推广
  • 自动做标题网站百度推广官网首页
  • 格尔木市公司网站建设各大搜索引擎入口
  • 合肥网站建设-中国互联网页seo是什么意思
  • 北京多用户商城网站建设网站推广的案例
  • 做网站的外包公司上班好不好seo研究协会网是干什么的
  • 下载免费网站模板下载网盘app下载
  • 网站前置审批怎么把网站排名到百度前三名
  • 做网站招商需要具备什么网站编辑怎么做
  • 环保政府网站建设目的意义营销存在的问题及改进