From 7b73c3e8401b82c4510243e46275539b65572c3f Mon Sep 17 00:00:00 2001 From: Lhchen <2244349522@qq.com> Date: Fri, 8 May 2026 09:26:28 +0000 Subject: [PATCH 01/19] =?UTF-8?q?rename=20=E7=BD=97=E7=9A=93=E6=99=A8/2026?= =?UTF-8?q?0429-Vue=E4=BA=8B=E4=BB=B6=E7=BB=91=E5=AE=9A&=E5=8F=8C=E5=90=91?= =?UTF-8?q?=E7=BB=91=E5=AE=9A.md=20to=20=E7=BD=97=E7=9A=93=E6=99=A8/202604?= =?UTF-8?q?29-Vue=E5=BE=85=E4=BF=AE=E6=94=B9=E5=BA=9F=E6=A1=8801.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lhchen <2244349522@qq.com> --- ...\276\205\344\277\256\346\224\271\345\272\237\346\241\21001.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\347\275\227\347\232\223\346\231\250/20260429-Vue\344\272\213\344\273\266\347\273\221\345\256\232&\345\217\214\345\220\221\347\273\221\345\256\232.md" => "\347\275\227\347\232\223\346\231\250/20260429-Vue\345\276\205\344\277\256\346\224\271\345\272\237\346\241\21001.md" (100%) diff --git "a/\347\275\227\347\232\223\346\231\250/20260429-Vue\344\272\213\344\273\266\347\273\221\345\256\232&\345\217\214\345\220\221\347\273\221\345\256\232.md" "b/\347\275\227\347\232\223\346\231\250/20260429-Vue\345\276\205\344\277\256\346\224\271\345\272\237\346\241\21001.md" similarity index 100% rename from "\347\275\227\347\232\223\346\231\250/20260429-Vue\344\272\213\344\273\266\347\273\221\345\256\232&\345\217\214\345\220\221\347\273\221\345\256\232.md" rename to "\347\275\227\347\232\223\346\231\250/20260429-Vue\345\276\205\344\277\256\346\224\271\345\272\237\346\241\21001.md" -- Gitee From de76b2b74ad3cfc6294b059b163b4aa28bbcf37c Mon Sep 17 00:00:00 2001 From: Lhchen <2244349522@qq.com> Date: Fri, 8 May 2026 09:26:44 +0000 Subject: [PATCH 02/19] =?UTF-8?q?rename=20=E7=BD=97=E7=9A=93=E6=99=A8/2026?= =?UTF-8?q?0430-Vue.md=20to=20=E7=BD=97=E7=9A=93=E6=99=A8/20260430-Vue?= =?UTF-8?q?=E5=BE=85=E4=BF=AE=E6=94=B9=E5=BA=9F=E6=A1=8802.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lhchen <2244349522@qq.com> --- ...\276\205\344\277\256\346\224\271\345\272\237\346\241\21002.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\347\275\227\347\232\223\346\231\250/20260430-Vue.md" => "\347\275\227\347\232\223\346\231\250/20260430-Vue\345\276\205\344\277\256\346\224\271\345\272\237\346\241\21002.md" (100%) diff --git "a/\347\275\227\347\232\223\346\231\250/20260430-Vue.md" "b/\347\275\227\347\232\223\346\231\250/20260430-Vue\345\276\205\344\277\256\346\224\271\345\272\237\346\241\21002.md" similarity index 100% rename from "\347\275\227\347\232\223\346\231\250/20260430-Vue.md" rename to "\347\275\227\347\232\223\346\231\250/20260430-Vue\345\276\205\344\277\256\346\224\271\345\272\237\346\241\21002.md" -- Gitee From 515caed12df33ce5f9e600de4687364e3cf15c52 Mon Sep 17 00:00:00 2001 From: Lhchen <2244349522@qq.com> Date: Sat, 9 May 2026 06:09:53 +0000 Subject: [PATCH 03/19] =?UTF-8?q?add=20=E7=BD=97=E7=9A=93=E6=99=A8/2026050?= =?UTF-8?q?7-Vue=E7=BB=84=E4=BB=B6.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lhchen <2244349522@qq.com> --- .../20260507-Vue\347\273\204\344\273\266.md" | 496 ++++++++++++++++++ 1 file changed, 496 insertions(+) create mode 100644 "\347\275\227\347\232\223\346\231\250/20260507-Vue\347\273\204\344\273\266.md" diff --git "a/\347\275\227\347\232\223\346\231\250/20260507-Vue\347\273\204\344\273\266.md" "b/\347\275\227\347\232\223\346\231\250/20260507-Vue\347\273\204\344\273\266.md" new file mode 100644 index 00000000..55cb58ad --- /dev/null +++ "b/\347\275\227\347\232\223\346\231\250/20260507-Vue\347\273\204\344\273\266.md" @@ -0,0 +1,496 @@ +## 笔记 + +### 组件基本定义与使用 + +#### 全局注册组件 + +在入口文件 main.js 中注册,所有页面和组件都能直接使用。 + +```javascript +// main.js +import { createApp } from 'vue' +import App from './App.vue' +// 导入组件 +import MyButton from './components/MyButton.vue' + +const app = createApp(App) +// 全局注册组件 +app.component('MyButton', MyButton) +app.mount('#app') +``` + +#### 局部注册组件 + +推荐优先使用局部注册,减少不必要的全局挂载,优化性能。 + +```vue + + + + +``` + +### 组件通信 + +#### 父子组件通信 + +##### Props(父 → 子 传值) + +父组件通过属性传递数据,子组件通过 defineProps 接收数据,支持类型校验、默认值、必填校验。 + +**子组件 Child.vue** + +```vue + + + +``` + +**父组件使用** + +```vue + + + +``` + +##### defineEmits(子 → 父 传值/触发事件) + +子组件通过自定义事件向父组件发送数据,父组件通过 v-on 监听事件。 + +**子组件Child.vue** + +```vue + + + +``` + +**父组件监听** + +```vue + + + +``` + +#### 跨级传值(provide / inject) + +用于祖孙组件、多级组件间传值,无需逐层传递(跳过中间组件),父组件(或祖先组件)用 provide 提供数据,子组件(或后代组件)用 inject 接收数据,支持响应式传值。 + +**祖先组件(提供数据)** + +```vue + +``` + +**后代组件(接收数据)** + +```vue + + + +``` + +### 组件核心能力(数据与生命周期) + +#### 组件响应式数据(组件内部状态管理) + +组件内部的响应式数据,用于维护组件自身的状态,仅在组件内部或通过通信传递给其他组件,常用 ref 和 reactive 定义。 + +```vue + + + +``` + +### 模板引用(获取 DOM/子组件实例) + +通过 ref 获取组件内部的 DOM 元素或子组件实例,实现组件内部 DOM 操作或调用子组件方法,是组件的常用能力。 + +```vue + + + +``` + +子组件暴露方法/属性(默认不暴露,需手动暴露) + +```vue + +``` + +### 动态组件 + +根据数据动态切换不同的组件,实现组件的灵活切换,适用于标签页、弹窗切换等场景。 + +```vue + + + +``` + +### 组件缓存 keep-alive + +缓存不活动的组件实例,避免组件重复创建和销毁,提升页面性能,常与动态组件搭配使用(缓存切换后的组件状态)。 + +```vue + +``` + +## 作业 + + +```vue + + + +``` + +```vue + + + +``` + -- Gitee From 928ca22d9cfbedd6a243ffc4df4efc0bec064374 Mon Sep 17 00:00:00 2001 From: Lhchen <2244349522@qq.com> Date: Sat, 9 May 2026 06:10:26 +0000 Subject: [PATCH 04/19] =?UTF-8?q?add=20=E7=BD=97=E7=9A=93=E6=99=A8/2026050?= =?UTF-8?q?8-Vue=E6=8F=92=E6=A7=BD.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lhchen <2244349522@qq.com> --- .../20260508-Vue\346\217\222\346\247\275.md" | 247 ++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 "\347\275\227\347\232\223\346\231\250/20260508-Vue\346\217\222\346\247\275.md" diff --git "a/\347\275\227\347\232\223\346\231\250/20260508-Vue\346\217\222\346\247\275.md" "b/\347\275\227\347\232\223\346\231\250/20260508-Vue\346\217\222\346\247\275.md" new file mode 100644 index 00000000..902d09d9 --- /dev/null +++ "b/\347\275\227\347\232\223\346\231\250/20260508-Vue\346\217\222\346\247\275.md" @@ -0,0 +1,247 @@ +## 笔记 + +### 插槽 Slot + +组件的内容分发机制,允许父组件向子组件传递 HTML 结构,使组件更灵活、可复用,是组件封装的重要能力。 + +#### 默认插槽 + +子组件预留一个默认插槽,父组件插入的内容会自动填充到该插槽。 + +子组件 MyBox.vue + +```vue + +``` + +父组件使用 + +```vue + +``` + +#### 具名插槽(多个插槽区分使用) + +子组件预留多个插槽,通过名称区分,父组件可针对性填充不同插槽内容。 + +**子组件** + +```vue + +``` + +**父组件使用** + +```vue + +``` + +#### 作用域插槽(子组件向插槽传数据) + +作用域插槽可以和具名插槽结合使用,给指定名称的插槽传递数据,父组件通过对应插槽名接收数据。 + +##### 默认作用域插槽 + +**子组件** + +```vue + + + +``` + +**父组件接收数据并渲染** + +```vue + +``` + +##### 具名作用域插槽 + +**子组件** + +```vue + + + +``` + +**父组件使用具名作用域插槽** + +```vue + + + +``` + +## 作业 + +```vue + + + + + +``` + -- Gitee From 7d9d1261080f203f7b82e1bcf9374b3b81f4169b Mon Sep 17 00:00:00 2001 From: Lhchen <2244349522@qq.com> Date: Mon, 11 May 2026 00:43:23 +0800 Subject: [PATCH 05/19] =?UTF-8?q?=E8=80=81=E8=99=8E=E8=80=81=E5=B8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20260506-Vue.md" | 48 + .../20260506-VueTypeScript.md" | 1239 +++++++++++++++++ 2 files changed, 1287 insertions(+) create mode 100644 "\347\275\227\347\232\223\346\231\250/20260506-Vue.md" create mode 100644 "\347\275\227\347\232\223\346\231\250/20260506-VueTypeScript.md" diff --git "a/\347\275\227\347\232\223\346\231\250/20260506-Vue.md" "b/\347\275\227\347\232\223\346\231\250/20260506-Vue.md" new file mode 100644 index 00000000..a487dca5 --- /dev/null +++ "b/\347\275\227\347\232\223\346\231\250/20260506-Vue.md" @@ -0,0 +1,48 @@ +```vue + + + + +``` + diff --git "a/\347\275\227\347\232\223\346\231\250/20260506-VueTypeScript.md" "b/\347\275\227\347\232\223\346\231\250/20260506-VueTypeScript.md" new file mode 100644 index 00000000..2bd36951 --- /dev/null +++ "b/\347\275\227\347\232\223\346\231\250/20260506-VueTypeScript.md" @@ -0,0 +1,1239 @@ +# 第一部分:开发环境与基础结构 + +## 基础配置 + +## 创建 Vue3 + TS 项目 + +```bash +# 推荐:Vite(当前Vue3主流方案) +npm create vite@latest my-vue-ts -- --template vue-ts + +# 安装依赖 +npm install + +# 启动项目 +npm run dev +``` + +------ + +# Vue3 + TS 基础组件结构 + +核心: + +- `lang="ts"` 开启 TypeScript +- ` + + +``` + +------ + +------ + +# 第二部分:TS 基础类型(Vue3 必学核心) + +## string + +```ts +const title: string = 'Vue3 + TS' + +const msg: string = `Hello Vue3` +``` + +适配场景: + +- Props 文本 +- 标题 +- 提示信息 +- 用户名 + +------ + +## number + +```ts +const age: number = 18 + +const price: number = 99.9 +``` + +适配场景: + +- ID +- 数量 +- 金额 +- 分页 + +------ + +## boolean + +```ts +const isShow: boolean = true + +const loading: boolean = false +``` + +适配场景: + +- loading +- 开关 +- 显示隐藏 +- 权限控制 + +------ + +## null 与 undefined + +```ts +const avatar: string | null = null + +const desc: string | undefined = undefined +``` + +Vue3 高频: + +```ts +const user = ref(null) +``` + +因为: + +- 接口数据未返回前为空 +- DOM 未挂载前为空 + +------ + +## void + +函数无返回值时使用。 + +```ts +const handleClick = (): void => { + console.log('点击') +} +``` + +------ + +## 数组类型 + +## 基础写法 + +```ts +const list: string[] = ['Vue', 'React'] +``` + +推荐写法: + +```ts +类型[] +``` + +这是 Vue3 中最常用写法。 + +------ + +## 泛型数组写法 + +```ts +const list: Array = ['Vue', 'React'] +``` + +等价于: + +```ts +string[] +``` + +------ + +## 对象数组(Vue3 超高频) + +```ts +interface User { + id: number + name: string +} + +const userList: User[] = [ + { + id: 1, + name: '张三' + }, + { + id: 2, + name: '李四' + } +] +``` + +适配场景: + +```vue +
  • + {{ item.name }} +
  • +``` + +------ + +------ + +# 第三部分:函数与对象类型(Vue3 开发核心) + +## 函数(Vue3 高频核心) + +Vue3 中函数极其高频: + +- 点击事件 +- 接口请求 +- 工具函数 +- emit +- computed +- hooks + +必须重点掌握。 + +------ + +## 基础函数 + +### 无参数无返回值 + +```ts +const closeDialog = (): void => { + console.log('关闭弹窗') +} +``` + +------ + +### 有参数无返回值 + +```ts +const deleteUser = (id: number): void => { + console.log(id) +} +``` + +------ + +### 有参数有返回值 + +```ts +const add = (a: number, b: number): number => { + return a + b +} +``` + +------ + +## 可选参数 + +```ts +const getName = ( + name: string, + age?: number +): string => { + if (age) { + return `${name}-${age}` + } + + return name +} +``` + +规则: + +- 可选参数必须放最后 + +错误: + +```ts +(name?: string, age: number) +``` + +------ + +## interface(Vue3 核心) + +Vue3 中: + +- Props +- API 数据 +- 表单 +- 用户信息 +- 商品数据 + +大量使用 interface。 + +------ + +## 基础对象类型 + +```ts +interface User { + id: number + name: string + age?: number +} +``` + +说明: + +- `?` 表示可选属性 + +------ + +## 使用 interface + +```ts +const user: User = { + id: 1, + name: '张三' +} +``` + +错误: + +```ts +const user: User = { + name: '张三' +} +``` + +因为缺少: + +```ts +id +``` + +------ + +## interface 继承 + +```ts +interface User { + name: string +} + +interface Admin extends User { + role: string +} +``` + +适配场景: + +- 用户体系 +- 接口扩展 +- 组件数据复用 + +------ + +## type(Vue3 高频) + +## 基础对象 + +```ts +type Product = { + id: number + title: string +} +``` + +------ + +## 联合类型(type 超高频) + +```ts +type Status = 'success' | 'error' | 'loading' +``` + +------ + +## 函数类型 + +```ts +type Fn = (name: string) => void +``` + +------ + +## interface 和 type 区别(Vue3 实战版) + +| 场景 | 推荐 | +| -------- | --------- | +| Props | interface | +| API数据 | interface | +| 联合类型 | type | +| 函数类型 | type | +| 工具类型 | type | +| 类型计算 | type | + +经验: + +- 对象结构优先 interface +- 联合/函数优先 type + +------ + +## 联合类型(Vue3 高频) + +## 基础联合类型 + +```ts +const id: string | number = 1001 +``` + +说明: + +只能是: + +- string +- number + +之一。 + +------ + +## 字面量联合(最常用) + +```ts +type ButtonType = + | 'primary' + | 'success' + | 'danger' +const type: ButtonType = 'primary' +``` + +错误: + +```ts +const type: ButtonType = 'warning' +``` + +------ + +## Vue3 高频场景 + +```ts +interface Props { + status: + | 'loading' + | 'success' + | 'error' +} +``` + +适配: + +- 状态管理 +- 按钮类型 +- 请求状态 +- 标签类型 + +------ + +------ + +# 第四部分:泛型与类型系统进阶 + +## 泛型(Vue3 + TS 核心) + +Vue3 大量 API 本质都是泛型。 + +例如: + +```ts +ref() +computed() +Promise +``` + +------ + +## 基础泛型 + +```ts +function getData(data: T): T { + return data +} +``` + +使用: + +```ts +getData('hello') + +getData(100) +``` + +------ + +## Promise 泛型 + +```ts +interface User { + name: string +} + +const getUser = async (): Promise => { + return { + name: '张三' + } +} +``` + +------ + +## 泛型约束 + +```ts +interface LengthType { + length: number +} + +function getLength( + data: T +): number { + return data.length +} +``` + +------ + +------ + +# 第五部分:Vue3 响应式 API + TS + +## Vue3 响应式 + TS + +这是 Vue3 + TS 真正核心。 + +------ + +### ref(最核心) + +## 基本类型 + +```ts +import { ref } from 'vue' + +const count = ref(0) + +count.value = 10 +``` + +特点: + +- 自动推导类型 +- 必须 `.value` + +------ + +## 字符串 + +```ts +const title = ref('Vue3') +``` + +------ + +## 布尔值 + +```ts +const loading = ref(false) +``` + +------ + +## 对象类型 + +```ts +interface User { + name: string + age: number +} + +const user = ref(null) +``` + +核心: + +```ts +User | null +``` + +因为: + +初始化为空。 + +------ + +## 数组类型 + +```ts +interface User { + name: string +} + +const userList = ref([]) +``` + +------ + +## never[] 易错点 + +错误: + +```ts +const list = ref([]) +``` + +会推导为: + +```ts +Ref +``` + +正确: + +```ts +const list = ref([]) +``` + +------ + +### reactive + +reactive: + +- 专门用于对象 +- 不需要 `.value` + +------ + +## 基础对象 + +```ts +import { reactive } from 'vue' + +interface User { + name: string + age: number +} + +const user = reactive({ + name: '张三', + age: 18 +}) +``` + +修改: + +```ts +user.age = 20 +``` + +------ + +## reactive 数组 + +```ts +const list = reactive([]) +``` + +------ + +### ref 和 reactive 区别 + +| 对比 | ref | reactive | +| -------------- | -------- | -------- | +| 基本类型 | ✅ | ❌ | +| 对象 | ✅ | ✅ | +| 数组 | ✅ | ✅ | +| 是否需要.value | ✅ | ❌ | +| 推荐场景 | 基本类型 | 对象结构 | + +经验: + +- 基本类型 → ref +- 对象 → reactive + +------ + +### computed(Vue3 高频) + +## 自动推导 + +```ts +const count = ref(10) + +const doubleCount = computed(() => { + return count.value * 2 +}) +``` + +自动推导: + +```ts +ComputedRef +``` + +------ + +## 手动指定类型 + +```ts +const title = computed(() => { + return 'Vue3 + TS' +}) +``` + +------ + +------ + +# 第六部分:Vue3 组件通信 + TS + +## Props + TS(核心) + +Vue3 + TS 最大核心之一。 + +------ + +## defineProps 基础写法 + +```ts +const props = defineProps({ + title: String, + + count: { + type: Number, + required: true + } +}) +``` + +------ + +### defineProps 泛型写法(推荐) + +## interface 定义 Props + +```ts +interface Props { + id: number + title: string + status?: 'success' | 'error' +} +``` + +------ + +## defineProps 泛型 + +```ts +const props = defineProps() +``` + +这是 Vue3 + TS 主流写法。 + +------ + +### Props 默认值 + +## withDefaults + +```ts +interface Props { + msg?: string + list?: string[] +} + +const props = withDefaults( + defineProps(), + { + msg: '默认消息', + + list: () => [] + } +) +``` + +重点: + +数组默认值必须: + +```ts +() => [] +``` + +不能直接: + +```ts +[] +``` + +------ + +## Emit + TS(组件通信核心) + +------ + +## defineEmits 基础写法 + +```ts +const emit = defineEmits([ + 'change', + 'update' +]) +``` + +缺点: + +- 不校验参数类型 + +------ + +### defineEmits 泛型写法(推荐) + +```ts +const emit = defineEmits<{ + change: [id: number] + + update: [ + title: string, + loading: boolean + ] + + confirm: [] +}>() +``` + +------ + +## 触发事件 + +```ts +emit('change', 1) + +emit('update', 'Vue3', true) + +emit('confirm') +``` + +错误: + +```ts +emit('change', '1') +``` + +因为: + +```ts +id: number +``` + +------ + +## defineExpose + TS + +Vue3 中: + + ``` #### 组件生命周期 - 组件从创建到销毁的整个过程,Vue3 组合式 API 中使用钩子函数监听,用于在组件不同阶段执行自定义逻辑,是组件的核心能力之一。 +- 直接导入使用,传入回调函数自动执行 +- 用于请求、操作DOM、清理定时器/监听 常用钩子函数: - - onMounted:组件挂载完成(DOM 渲染完毕,适合发起网络请求、操作 DOM) - onUpdated:组件更新完成(组件数据变化导致 DOM 重新渲染后执行) - onUnmounted:组件卸载(组件销毁时执行,适合清除定时器、事件监听、取消请求) @@ -229,8 +234,11 @@ onUnmounted(() => { ``` ### 模板引用(获取 DOM/子组件实例) - -通过 ref 获取组件内部的 DOM 元素或子组件实例,实现组件内部 DOM 操作或调用子组件方法,是组件的常用能力。 +通过 ref 获取组件内部的 DOM 元素或子组件实例,实现组件内部 DOM 操作或调用子组件方法。 +- 模板:`ref="变量名"` +- 脚本:`const 变量名 = ref(null)` +- 挂载后通过 `.value` 获取真实DOM / 子组件实例 +- 子组件必须用 `defineExpose` 暴露方法才能被调用 ```vue