首页
Search
1
剑侠账本
351 阅读
2
2024最好用的虚拟卡 VISA
320 阅读
3
使用vue3及Echarts插件实现Github代码热力图
318 阅读
4
Mac 开机自动执行 Shll 脚本
253 阅读
5
Nginx配置后请求报404的解决思路
194 阅读
Vue
React
Nodejs
小程序
工具
其他
随笔
登录
Search
标签搜索
Vue
Shll
vue3
echarts
ts
typescript
eventbus
Visa
Nginx
Nodejs
Aoki
累计撰写
10
篇文章
累计收到
11
条评论
首页
栏目
Vue
React
Nodejs
小程序
工具
其他
随笔
页面
搜索到
3
篇与
的结果
2024-03-15
Vue中的Event Bus(事件总线)
Vue中的Event Bus(事件总线)在Vue中,使用event bus是一种常见的实现组件间通信的方法。Event bus是一个空的Vue实例,用于在应用程序内部传递事件。本篇博客将介绍如何创建和使用Vue event bus。创建event bus首先,需要创建一个event bus,可以将其定义为一个单独的文件,并在应用程序中引入:// EventBus.js 文件中 import Vue from 'vue' export default new Vue()在这个示例中,我们将new Vue()的实例作为event bus,然后通过export default将其导出。发送事件在发送方组件中,可以通过this.$emit()方法触发一个自定义事件,并将需要传递的数据作为参数传递:// 发送方组件中 import EventBus from '@/path/to/EventBus.js' methods: { handleClick() { // 触发自定义事件,并传递参数 EventBus.$emit('my-event', 'hello from sender') } }在这个示例中,我们通过EventBus.$emit()方法触发了一个名为my-event的自定义事件,并向其中传递了一个字符串类型的参数'hello from sender'。接收事件在接收方组件中,可以通过EventBus.$on()方法订阅该自定义事件并处理其响应:// 接收方组件中 import EventBus from '@/path/to/EventBus.js' mounted() { // 订阅自定义事件 EventBus.$on('my-event', this.handleMyEvent) }, methods: { handleMyEvent(msg) { console.log(msg) } }在这个示例中,我们在接收方组件的mounted()生命周期钩子函数中订阅了名为my-event的自定义事件,并指定其响应方法为handleMyEvent()。当该自定义事件被触发时,handleMyEvent()方法就会被调用,并将接收到的参数打印到控制台中。结论通过event bus可以使得不同层级的组件之间进行通信,而不需要通过props属性或slot来传递数据。但是,event bus过渡使用也可能导致代码难以维护和追踪。因此,在实现组件间通信时,应该根据具体情况选择合适的方法,并避免过度依赖某一种方法。
2024年03月15日
166 阅读
0 评论
1 点赞
2024-03-15
Vue 3.0 + TypeScript: 从配置到实践
Vue 3.0 + TypeScript: 从配置到实践在Vue 3.0中引入了TypeScript(TS)语言支持,这意味着我们可以使用TypeScript来编写Vue应用程序。在本篇博客中,我们将介绍如何为Vue 3.0项目配置TypeScript,并提供一些实用的代码示例。安装TypeScript首先,我们需要安装TypeScript。可以使用以下命令来全局安装TypeScript:npm install -g typescript然后,在我们的Vue项目中安装TypeScript依赖:npm install --save-dev typescript配置TypeScript接下来,我们需要配置TypeScript来与Vue一起使用。首先,创建一个tsconfig.json文件,以告诉TypeScript如何编译Vue代码。{ "compilerOptions": { "target": "es6", "module": "esnext", "moduleResolution": "node", "strict": true, "jsx": "preserve", "sourceMap": true, "resolveJsonModule": true, "esModuleInterop": true, "noImplicitReturns": true, "noImplicitThis": true, "noImplicitAny": false, "strictNullChecks": true, "suppressImplicitAnyIndexErrors": true, "declaration": true, "baseUrl": ".", "paths": { "@/*": ["src/*"] } }, "include": ["src/**/*.ts", "src/**/*.tsx", "tests/**/*.ts", "tests/**/*.tsx", "typings/**/*.d.ts"], "exclude": ["node_modules"] }解释"target": "es6":编译代码的目标是ES6"module": "esnext":使用ES Modules来组织代码,这也是Vue 3.0推荐的方式"strict": true:启用所有严格的类型检查选项"jsx": "preserve":保留JSX以供后面的编译步骤进行处理"sourceMap": true:生成源码映射文件,方便调试"resolveJsonModule": true:允许导入JSON文件"esModuleInterop": true:允许在CommonJS模块中导入默认导出"noImplicitReturns": true,"noImplicitThis": true,"noImplicitAny": false,"strictNullChecks": true:更严格的类型检查选项集成TS与Vue现在我们已经为Vue项目配置了TypeScript,接下来需要做的就是将TypeScript集成到Vue中。我们需要做以下两个步骤:1. 修改.vue文件在Vue 3.0中,单文件组件(SFC)可以包含TypeScript代码,但需要指定标签的lang属性为ts,如下所示:<script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ name: 'HelloWorld', props: { msg: String } }) </script>2. 编写Vue插件如果你想编写可重用的Vue插件,你需要创建一个.d.ts文件来为插件提供类型定义。例如,如果我们编写了一个名为MyPlugin的Vue插件,我们可以在my-plugin.d.ts文件中导出插件选项:import { Plugin } from 'vue' declare const MyPlugin: Plugin export default MyPlugin示例代码下面是一个使用TypeScript编写的简单Vue组件示例:<template> <div class="hello"> {{ message }} </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ name: 'HelloWorld', props: { msg: String }, data() { return { message: 'Hello World!' } } }) </script>结论通过以上步骤,我们已经成功地将TypeScript集成到Vue 3.0项目中。使用TypeScript可以确保
2024年03月15日
170 阅读
0 评论
0 点赞
2024-03-15
关于Html使用CDN引入ElementUI-Plus与Vue3(组合式)不生效解决方法
关于Html使用CDN引入ElementUI-Plus与Vue3(组合式)不生效解决方法新版的需要挂载一下<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link href="https://cdn.bootcdn.net/ajax/libs/element-plus/2.3.12/index.min.css" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.3.4/vue.global.prod.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/element-plus/2.3.12/index.full.js"></script> </head> <body> <div id="app"> <el-button>{{message}}</el-button> </div> </body> <script> const { createApp, ref } = Vue createApp({ setup () { const message = ref('Hello vue!') return { message, } } }).use(ElementPlus).mount('#app') </script> </html>需要.use(ElementPlus)
2024年03月15日
170 阅读
0 评论
2 点赞