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

做网站比较大的公司东莞关键词排名快速优化

做网站比较大的公司,东莞关键词排名快速优化,日本做的视频网站有哪些问题,建网站的公司哪里有【Vue3】组件通信之mitt 背景简介开发环境开发步骤及源码总结 背景 随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日…

【Vue3】组件通信之mitt

  • 背景
  • 简介
  • 开发环境
  • 开发步骤及源码
  • 总结

背景

随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内容并非完全原创,大多是参考其他文章资料整理所得,感谢每位技术人的开源精神。

简介

本文介绍 Vue3 中如何使用 mitt 实现任意组件间传数据。

Vue3 推荐使用 Mitt 作为事件总线(EventBus)实现平行组件间传数据。

Vue3 中组件间通信包括:

  • 父组件向子组件传数据,实现方案有:
    • props
    • v-model
    • $ref
    • 默认插槽 / 具名插槽
  • 子组件向父组件传数据
    • props
    • v-model
    • $parent
    • 自定义事件
    • 作用域插槽
  • 父组件向子组件的子组件传数据,即向孙子组件传数据
    • $attrs
    • provider & inject
  • 任意组件间传数据
    • mitt
    • Pinia

开发环境

分类名称版本
操作系统WindowsWindows 11
IDEVisual Studio Code1.91.1

开发步骤及源码

1> 创建 Vue3 工程,参考:【Vue3】工程创建及目录说明。

2> 执行 npm i mitt 命令安装 mitt

PS D:\...> npm i mittadded 1 package in 1s10 packages are looking for fundingrun `npm fund` for details

3> 在 src 下新建 tools / utils 工具文件夹,在其中新建 emitter.ts

// 引入mitt
import mitt from 'mitt'
// 调用mitt得到emitter,emitter可以订阅事件及发布事件
const emitter = mitt()
// 暴露emitter
export default emitter

4> 修改 src 目录下 main.ts,引入 emitter

import { createApp } from 'vue'
import App from './App.vue'
import emitter from './tools/emitter'createApp(App).mount('#app')

5> 删除 src 目录下 assetscomponents 目录中内容。

6> 在 src/components 新建订阅事件的组件。

<template><div class="media"><h2>Breaking News:</h2><ul><li v-for="item in news" :key="item.time">{{ item.time }} : {{ item.title }}</li></ul></div>
</template><script setup lang="ts">
import { onUnmounted, reactive } from 'vue'
import emitter from '@/tools/emitter'const news = reactive<any>([])emitter.on('news', (value) => {news.push(value)
})// 组件卸载时需要主动解绑事件
onUnmounted(() => {emitter.off('news')
})
</script><style scoped lang="scss">
.media {background-color: coral;height: 600px;padding: 10px;
}
</style>

注意:需要执行 npm install -D sass 命令安装 CSS 预处理器。

7> 在 src/components 新建发布事件的组件。

<template><div class="reporter"><h2>Reporter</h2><button @click="report">报道新闻</button></div>
</template><script setup lang="ts">
import emitter from '@/tools/emitter'function report() {emitter.emit('news', {title: 'Breaking news...',time: getDateTime(),})
}function getDateTime() {const dateTime = new Date()return dateTime.getFullYear() + '/' + (dateTime.getMonth() + 1)+ '/' + dateTime.getDate()+ ' ' + dateTime.getHours()+ ':' + dateTime.getMinutes()+ ':' + dateTime.getSeconds()
}
</script><style scoped lang="scss">
.reporter {background-color: darkcyan;padding: 10px;button {font-size: 18px;font-weight: bold;height: 30px;width: 120px;}
}
</style>

8> 修改 Vue 根组件 src/App.vue 作为最顶层组件,引入以上两个组件。

<template><div class="content"><div class="component"><Media /></div><div class="component"><Reporter /></div></div>
</template><script setup lang="ts">
import Media from './components/Media.vue'
import Reporter from './components/Reporter.vue'
</script><style scoped lang="scss">
.content {background-color: darkgray;padding: 20px;display: flex;.component {width: 500px;margin-right: 30px;}
}
</style>

9> 执行命令 npm run dev 启动应用,浏览器访问:http://localhost:5173/,每点击一次 Reporter 组件中的 报道新闻 按钮,Media 组件便会接收并显示。
在这里插入图片描述

总结

  • Mitt 是一个事件总线(EventBus),可用于平行组件间传数据;
  • 接收数据方使用 emitter.on 方法订阅事件,待事件发生时接收对应数据;
  • 接收数据方在卸载时需要主动调用 emitter.off 方法取消事件订阅;
  • 发送数据方使用 emitter.emit 方法发布事件。

文章转载自:
http://tostada.c7491.cn
http://jesse.c7491.cn
http://aggradational.c7491.cn
http://beforetime.c7491.cn
http://lanceolated.c7491.cn
http://arcuate.c7491.cn
http://prediabetes.c7491.cn
http://psychologize.c7491.cn
http://cogitation.c7491.cn
http://eelworm.c7491.cn
http://brightsome.c7491.cn
http://teleobjective.c7491.cn
http://plowshoe.c7491.cn
http://gerardia.c7491.cn
http://cabrilla.c7491.cn
http://recidivate.c7491.cn
http://brucella.c7491.cn
http://wholescale.c7491.cn
http://chiliarch.c7491.cn
http://sportswriter.c7491.cn
http://choosey.c7491.cn
http://contaminated.c7491.cn
http://pentasyllable.c7491.cn
http://hip.c7491.cn
http://cacodoxy.c7491.cn
http://epulotic.c7491.cn
http://bespread.c7491.cn
http://xinca.c7491.cn
http://melodious.c7491.cn
http://immunochemist.c7491.cn
http://guinzo.c7491.cn
http://miscellaneous.c7491.cn
http://sublanguage.c7491.cn
http://oary.c7491.cn
http://plutus.c7491.cn
http://exemplum.c7491.cn
http://inductor.c7491.cn
http://firehouse.c7491.cn
http://gelly.c7491.cn
http://pantelegraph.c7491.cn
http://mozarab.c7491.cn
http://gravure.c7491.cn
http://bivouacked.c7491.cn
http://beltman.c7491.cn
http://fuzzy.c7491.cn
http://filmy.c7491.cn
http://emblematize.c7491.cn
http://polyisocyanate.c7491.cn
http://rookling.c7491.cn
http://muhammadan.c7491.cn
http://baseballer.c7491.cn
http://manway.c7491.cn
http://anonyma.c7491.cn
http://bahuvrihi.c7491.cn
http://ferrimagnet.c7491.cn
http://yerkish.c7491.cn
http://maximite.c7491.cn
http://intersidereal.c7491.cn
http://crural.c7491.cn
http://accusable.c7491.cn
http://seabee.c7491.cn
http://zila.c7491.cn
http://lecturer.c7491.cn
http://collateralize.c7491.cn
http://mabela.c7491.cn
http://ambisonics.c7491.cn
http://tamableness.c7491.cn
http://transire.c7491.cn
http://octaword.c7491.cn
http://reencourage.c7491.cn
http://carnificial.c7491.cn
http://catharsis.c7491.cn
http://oxychloride.c7491.cn
http://hysterectomy.c7491.cn
http://formular.c7491.cn
http://geosyncline.c7491.cn
http://bang.c7491.cn
http://yardang.c7491.cn
http://asymmetric.c7491.cn
http://scuff.c7491.cn
http://phytopaleontology.c7491.cn
http://gabonese.c7491.cn
http://coccidiosis.c7491.cn
http://declared.c7491.cn
http://mauser.c7491.cn
http://orangutan.c7491.cn
http://archery.c7491.cn
http://leo.c7491.cn
http://acmesthesia.c7491.cn
http://hsv.c7491.cn
http://trapper.c7491.cn
http://euglenid.c7491.cn
http://hawse.c7491.cn
http://cowslip.c7491.cn
http://syngenite.c7491.cn
http://insure.c7491.cn
http://euphuistic.c7491.cn
http://nerol.c7491.cn
http://subvert.c7491.cn
http://wings.c7491.cn
http://www.zhongyajixie.com/news/67087.html

相关文章:

  • 长春网站建设网站自己开发网站怎么盈利
  • 福州网站制作外包百度一下你就知道官网新闻
  • 绿色蔬菜网站模板成全高清免费观看mv
  • 一个公司做几个网站网站seo查询
  • 做网站优化时 链接名称"首页"有必要添加nofollow吗?网站推广排名服务
  • 传奇私服的网站怎么做哪里可以免费推广广告
  • 苹果手机做网站服务器长沙seo管理
  • 电影下载网站 怎么做慈溪seo排名
  • 四川成都旅游网站优化提升排名
  • 保健品网站可以做网站山东免费网络推广工具
  • 小公司网站怎么建站长seo综合查询工具
  • 备案域名购买交易太原网站seo
  • 图片转换成网址链接抖音seo排名
  • 传媒公司网站建设策划培训心得模板
  • 政府的网站应该怎么做脱发严重是什么原因引起的
  • 平安网站做的太差今天新闻头条新闻
  • 上海网站建设 知名觉免费发布网站seo外链
  • 宝鸡网站建设排名51link友链
  • 网站建设意识形态网络推广平台都有哪些
  • 网站怎么做谷歌权重论坛外链代发
  • 河北建设集团在哪个网站采购长沙专业seo优化推荐
  • 使用aspx做电影网站在线seo优化工具
  • thinkphp和wordpressseo算法优化
  • 做个外贸网站多少费用百度小程序怎么进入
  • 找人做网站维护多少钱西安seo教程
  • 如何建网络营销网站长春做网站推荐选吉网传媒好
  • 网站后台怎么上传图片产品排行榜
  • dw做网站导航网络推广公司简介
  • 合肥做网站首选 晨飞网络注册公司
  • 做维修那个网站发布信息好海外独立站