diff --git a/Vue/code/01.源码/App.vue b/Vue/code/01.源码/App.vue new file mode 100644 index 00000000..de86f518 --- /dev/null +++ b/Vue/code/01.源码/App.vue @@ -0,0 +1,28 @@ + + + + + diff --git a/Vue/code/01.源码/assets/logo.png b/Vue/code/01.源码/assets/logo.png new file mode 100644 index 00000000..f3d2503f Binary files /dev/null and b/Vue/code/01.源码/assets/logo.png differ diff --git a/Vue/code/01.源码/components/HelloWorld.vue b/Vue/code/01.源码/components/HelloWorld.vue new file mode 100644 index 00000000..5b06cac2 --- /dev/null +++ b/Vue/code/01.源码/components/HelloWorld.vue @@ -0,0 +1,61 @@ + + + + + + diff --git a/Vue/code/01.源码/main.ts b/Vue/code/01.源码/main.ts new file mode 100644 index 00000000..01433bca --- /dev/null +++ b/Vue/code/01.源码/main.ts @@ -0,0 +1,4 @@ +import { createApp } from 'vue' +import App from './App.vue' + +createApp(App).mount('#app') diff --git a/Vue/code/01.源码/shims-vue.d.ts b/Vue/code/01.源码/shims-vue.d.ts new file mode 100644 index 00000000..3804a43e --- /dev/null +++ b/Vue/code/01.源码/shims-vue.d.ts @@ -0,0 +1,6 @@ +/* eslint-disable */ +declare module '*.vue' { + import type { DefineComponent } from 'vue' + const component: DefineComponent<{}, {}, any> + export default component +} diff --git a/Vue/code/02.setup初识/App.vue b/Vue/code/02.setup初识/App.vue new file mode 100644 index 00000000..85ebd817 --- /dev/null +++ b/Vue/code/02.setup初识/App.vue @@ -0,0 +1,20 @@ + + + diff --git a/Vue/code/03.setup和ref的基本使用/App.vue b/Vue/code/03.setup和ref的基本使用/App.vue new file mode 100644 index 00000000..203f29a6 --- /dev/null +++ b/Vue/code/03.setup和ref的基本使用/App.vue @@ -0,0 +1,56 @@ + + + diff --git a/Vue/code/04.reactive基本使用/App.vue b/Vue/code/04.reactive基本使用/App.vue new file mode 100644 index 00000000..a5233ae2 --- /dev/null +++ b/Vue/code/04.reactive基本使用/App.vue @@ -0,0 +1,78 @@ + + + diff --git a/Vue/code/05.vue3的响应式数据的核心原理/index.html b/Vue/code/05.vue3的响应式数据的核心原理/index.html new file mode 100644 index 00000000..b927b99e --- /dev/null +++ b/Vue/code/05.vue3的响应式数据的核心原理/index.html @@ -0,0 +1,57 @@ + + + + + + + vue3的响应式数据的核心原理 + + + + + + diff --git a/Vue/code/06.setup的细节问题/App.vue b/Vue/code/06.setup的细节问题/App.vue new file mode 100644 index 00000000..9f2da183 --- /dev/null +++ b/Vue/code/06.setup的细节问题/App.vue @@ -0,0 +1,31 @@ + + + diff --git a/Vue/code/06.setup的细节问题/components/Child.vue b/Vue/code/06.setup的细节问题/components/Child.vue new file mode 100644 index 00000000..c8e06495 --- /dev/null +++ b/Vue/code/06.setup的细节问题/components/Child.vue @@ -0,0 +1,79 @@ + + + diff --git a/Vue/code/07.ref和reactive的细节问题/App.vue b/Vue/code/07.ref和reactive的细节问题/App.vue new file mode 100644 index 00000000..0eacac57 --- /dev/null +++ b/Vue/code/07.ref和reactive的细节问题/App.vue @@ -0,0 +1,55 @@ + + diff --git a/Vue/code/08.watch和watchEffect/App.vue b/Vue/code/08.watch和watchEffect/App.vue new file mode 100644 index 00000000..7d62108f --- /dev/null +++ b/Vue/code/08.watch和watchEffect/App.vue @@ -0,0 +1,97 @@ + + + diff --git a/Vue/code/09.生命周期/App.vue b/Vue/code/09.生命周期/App.vue new file mode 100644 index 00000000..4adca43c --- /dev/null +++ b/Vue/code/09.生命周期/App.vue @@ -0,0 +1,25 @@ + + + diff --git a/Vue/code/09.生命周期/components/Child.vue b/Vue/code/09.生命周期/components/Child.vue new file mode 100644 index 00000000..8ec8bad0 --- /dev/null +++ b/Vue/code/09.生命周期/components/Child.vue @@ -0,0 +1,70 @@ + + diff --git a/Vue/code/09.生命周期/lifecycle/lifecycle2.png b/Vue/code/09.生命周期/lifecycle/lifecycle2.png new file mode 100644 index 00000000..f8cd62c5 Binary files /dev/null and b/Vue/code/09.生命周期/lifecycle/lifecycle2.png differ diff --git a/Vue/code/09.生命周期/lifecycle/lifecycle3.png b/Vue/code/09.生命周期/lifecycle/lifecycle3.png new file mode 100644 index 00000000..36dd7107 Binary files /dev/null and b/Vue/code/09.生命周期/lifecycle/lifecycle3.png differ diff --git a/Vue/code/10.自定义hooks/App.vue b/Vue/code/10.自定义hooks/App.vue new file mode 100644 index 00000000..07908d88 --- /dev/null +++ b/Vue/code/10.自定义hooks/App.vue @@ -0,0 +1,64 @@ + + + diff --git a/Vue/code/10.自定义hooks/hooks/useMousePosition.ts b/Vue/code/10.自定义hooks/hooks/useMousePosition.ts new file mode 100644 index 00000000..9671d7be --- /dev/null +++ b/Vue/code/10.自定义hooks/hooks/useMousePosition.ts @@ -0,0 +1,28 @@ +import { ref, onMounted, onUnmounted } from 'vue' + +export default function() { + const x = ref(0) + const y = ref(0) + + // 点击事件的回调函数 + const onHandlerClick = (event: MouseEvent) => { + x.value = event.pageX + y.value = event.pageY + } + + // 页面已经加载完毕了,再进行点击的操作 + // 页面加载完毕的生命周期组合API + onMounted(() => { + window.addEventListener('click', onHandlerClick) + }) + + // 页面卸载之前的生命周期组合API + onUnmounted(() => { + window.removeEventListener('click', onHandlerClick) + }) + + return { + x, + y + } +} diff --git a/Vue/code/10.自定义hooks/hooks/useRequest.ts b/Vue/code/10.自定义hooks/hooks/useRequest.ts new file mode 100644 index 00000000..d9a2bdbd --- /dev/null +++ b/Vue/code/10.自定义hooks/hooks/useRequest.ts @@ -0,0 +1,29 @@ +import { ref } from 'vue' +import axios from 'axios' + +// 发送 ajax 请求 +export default function(url: string) { + // 加载的状态 + const loading = ref(true) + // 请求成功的数据 + const data = ref(null) // 坑 + // 错误信息 + const errorMsg = ref('') + + axios + .get(url) + .then(response => { + loading.value = false + data.value = response.data + }) + .catch(error => { + loading.value = false + errorMsg.value = error.message || '未知错误' + }) + + return { + loading, + data, + errorMsg + } +} diff --git a/Vue/code/11.toRefs/App.vue b/Vue/code/11.toRefs/App.vue new file mode 100644 index 00000000..8396fc38 --- /dev/null +++ b/Vue/code/11.toRefs/App.vue @@ -0,0 +1,67 @@ + + + diff --git a/Vue/code/12.ref获取页面元素/App.vue b/Vue/code/12.ref获取页面元素/App.vue new file mode 100644 index 00000000..101dcfa9 --- /dev/null +++ b/Vue/code/12.ref获取页面元素/App.vue @@ -0,0 +1,26 @@ + + + diff --git a/Vue/code/13.shallowReactive和shallowRef/App.vue b/Vue/code/13.shallowReactive和shallowRef/App.vue new file mode 100644 index 00000000..70a24f90 --- /dev/null +++ b/Vue/code/13.shallowReactive和shallowRef/App.vue @@ -0,0 +1,87 @@ + + + diff --git a/Vue/code/14.readonly和shallowReadonly/App.vue b/Vue/code/14.readonly和shallowReadonly/App.vue new file mode 100644 index 00000000..a9ebdae0 --- /dev/null +++ b/Vue/code/14.readonly和shallowReadonly/App.vue @@ -0,0 +1,43 @@ + + + diff --git a/Vue/code/15.toRaw和markRaw/App.vue b/Vue/code/15.toRaw和markRaw/App.vue new file mode 100644 index 00000000..525705dd --- /dev/null +++ b/Vue/code/15.toRaw和markRaw/App.vue @@ -0,0 +1,55 @@ + + + diff --git a/Vue/code/16_toRef的使用/App.vue b/Vue/code/16_toRef的使用/App.vue new file mode 100644 index 00000000..e2af7184 --- /dev/null +++ b/Vue/code/16_toRef的使用/App.vue @@ -0,0 +1,54 @@ + + + + diff --git a/Vue/code/16_toRef的使用/components/Child.vue b/Vue/code/16_toRef的使用/components/Child.vue new file mode 100644 index 00000000..329928c2 --- /dev/null +++ b/Vue/code/16_toRef的使用/components/Child.vue @@ -0,0 +1,30 @@ + + + diff --git a/Vue/code/17.customRef的使用/App.vue b/Vue/code/17.customRef的使用/App.vue new file mode 100644 index 00000000..10dab97b --- /dev/null +++ b/Vue/code/17.customRef的使用/App.vue @@ -0,0 +1,46 @@ + + + diff --git a/Vue/code/18.provide与inject/App.vue b/Vue/code/18.provide与inject/App.vue new file mode 100644 index 00000000..091fd85b --- /dev/null +++ b/Vue/code/18.provide与inject/App.vue @@ -0,0 +1,33 @@ + + + diff --git a/Vue/code/18.provide与inject/components/GrandSon.vue b/Vue/code/18.provide与inject/components/GrandSon.vue new file mode 100644 index 00000000..0bed7b63 --- /dev/null +++ b/Vue/code/18.provide与inject/components/GrandSon.vue @@ -0,0 +1,18 @@ + + + diff --git a/Vue/code/18.provide与inject/components/Son.vue b/Vue/code/18.provide与inject/components/Son.vue new file mode 100644 index 00000000..203af6c7 --- /dev/null +++ b/Vue/code/18.provide与inject/components/Son.vue @@ -0,0 +1,17 @@ + + + diff --git a/Vue/code/19.响应式数据判断的相关方法/App.vue b/Vue/code/19.响应式数据判断的相关方法/App.vue new file mode 100644 index 00000000..8f8a6b15 --- /dev/null +++ b/Vue/code/19.响应式数据判断的相关方法/App.vue @@ -0,0 +1,29 @@ + + + diff --git a/Vue/code/20.手写组合API/index.html b/Vue/code/20.手写组合API/index.html new file mode 100644 index 00000000..37e91f33 --- /dev/null +++ b/Vue/code/20.手写组合API/index.html @@ -0,0 +1,110 @@ + + + + + + 20.手写组合API + + + + + + diff --git a/Vue/code/20.手写组合API/index.js b/Vue/code/20.手写组合API/index.js new file mode 100644 index 00000000..dc5c42e7 --- /dev/null +++ b/Vue/code/20.手写组合API/index.js @@ -0,0 +1,165 @@ +// shallowReactive(浅的劫持,浅的监视,浅的响应数据) 与 reactive(深的) + +// TODO 68_尚硅谷_Vue3-手写shallowReactive和reactive +// 定义一个reactiveHandler处理对象 +const reactiveHandler = { + // 获取属性值 + get(target, prop) { + if (prop === '_is_reactive') return true + const result = Reflect.get(target, prop) + console.log('拦截了读取数据', prop, result) + return result + }, + // 修改属性值或者是添加属性 + set(target, prop, value) { + const result = Reflect.set(target, prop, value) + console.log('拦截了修改数据或者是添加属性', prop, value) + return result + }, + // 删除某个属性 + deleteProperty(target, prop) { + const result = Reflect.deleteProperty(target, prop) + console.log('拦截了删除数据', prop) + return result + } +} + +// 定义一个shallowReactive函数,传入一个目标对象 +function shallowReactive(target) { + // 判断当前的目标对象是不是object类型(对象/数组) + if (target && typeof target === 'object') { + return new Proxy(target, reactiveHandler) + } + // 如果传入的数据是基本类型的数据,那么就直接返回 + return target +} + +// 定义一个reactive函数,传入一个目标对象 +function reactive(target) { + // 判断当前的目标对象是不是object类型(对象/数组) + if (target && typeof target === 'object') { + // 对数组或者是对象中所有的数据进行reactive的递归处理 + // 先判断当前的数据是不是数组 + if (Array.isArray(target)) { + // 数组的数据要进行遍历操作 + target.forEach((item, index) => { + target[index] = reactive(item) + }) + } else { + // 再判断当前的数据是不是对象 + // 对象的数据也要进行遍历的操作 + Object.keys(target).forEach(key => { + target[key] = reactive(target[key]) + }) + } + return new Proxy(target, reactiveHandler) + } + // 如果传入的数据是基本类型的数据,那么就直接返回 + return target +} + +// TODO 69_尚硅谷_Vue3-手写shallowReadonly和readonly +// 定义了一个readonlyHandler处理器 +const readonlyHandler = { + get(target, prop) { + if (prop === '_is_readonly') return true + const result = Reflect.get(target, prop) + console.log('拦截到了读取数据了', prop, result) + return result + }, + set(target, prop, value) { + console.warn('只能读取数据,不能修改数据或者添加数据') + return true + }, + deleteProperty(target, prop) { + console.warn('只能读取数据,不能删除数据') + return true + } +} + +// 定义一个shallowReadonly函数 +function shallowReadonly(target) { + // 需要判断当前的数据是不是对象 + if (target && typeof target === 'object') { + return new Proxy(target, readonlyHandler) + } + return target +} + +// 定义一个readonly函数 +function readonly(target) { + // 需要判断当前的数据是不是对象 + if (target && typeof target === 'object') { + // 判断target是不是数组 + if (Array.isArray(target)) { + // 遍历数组 + target.forEach((item, index) => { + target[index] = readonly(item) + }) + } else { + // 判断target是不是对象 + // 遍历对象 + Object.keys(target).forEach(key => { + target[key] = readonly(target[key]) + }) + } + return new Proxy(target, readonlyHandler) + } + // 如果不是对象或者数组,那么直接返回 + return target +} + +// TODO 70_尚硅谷_Vue3-手写 shallowRef 和 ref + +// 定义一个shallowRef函数 +function shallowRef(target) { + return { + // 保存target数据保存起来 + _value: target, + get value() { + console.log('劫持到了读取数据') + return this._value + }, + set value(val) { + console.log('劫持到了修改数据,准备更新界面', val) + this._value = val + } + } +} + +// 定义一个ref函数 +function ref(target) { + target = reactive(target) + return { + _is_ref: true, // 标识当前的对象是ref对象 + // 保存target数据保存起来 + _value: target, + get value() { + console.log('劫持到了读取数据') + return this._value + }, + set value(val) { + console.log('劫持到了修改数据,准备更新界面', val) + this._value = val + } + } +} + +// TODO 71_尚硅谷_Vue3-手写isRef和isReactive和isReadonly + +// 定义一个函数isRef,判断当前的对象是不是ref对象 +function isRef(obj) { + return obj && obj._is_ref +} +// 定义一个函数isReactive,判断当前的对象是不是reactive对象 +function isReactive(obj) { + return obj && obj._is_reactive +} +// 定义一个函数isReadonly,判断当前的对象是不是readonly对象 +function isReadonly(obj) { + return obj && obj._is_readonly +} +// 定义一个函数isProxy,判断当前的对象是不是reactive对象或者readonly对象 +function isProxy(obj) { + return isReactive(obj) || isReadonly(obj) +} diff --git a/Vue/code/21.Fragment组件的介绍/App.vue b/Vue/code/21.Fragment组件的介绍/App.vue new file mode 100644 index 00000000..816c59db --- /dev/null +++ b/Vue/code/21.Fragment组件的介绍/App.vue @@ -0,0 +1,21 @@ + + + diff --git a/Vue/code/22.Teleport组件介绍/App.vue b/Vue/code/22.Teleport组件介绍/App.vue new file mode 100644 index 00000000..5492d997 --- /dev/null +++ b/Vue/code/22.Teleport组件介绍/App.vue @@ -0,0 +1,17 @@ + + + diff --git a/Vue/code/22.Teleport组件介绍/ModalButton.vue b/Vue/code/22.Teleport组件介绍/ModalButton.vue new file mode 100644 index 00000000..74b4ca8c --- /dev/null +++ b/Vue/code/22.Teleport组件介绍/ModalButton.vue @@ -0,0 +1,55 @@ + + + + + + + diff --git a/Vue/code/23.Suspense组件的使用/App.vue b/Vue/code/23.Suspense组件的使用/App.vue new file mode 100644 index 00000000..8f800433 --- /dev/null +++ b/Vue/code/23.Suspense组件的使用/App.vue @@ -0,0 +1,36 @@ + + + diff --git a/Vue/code/23.Suspense组件的使用/AsyncAddress.vue b/Vue/code/23.Suspense组件的使用/AsyncAddress.vue new file mode 100644 index 00000000..6bb87831 --- /dev/null +++ b/Vue/code/23.Suspense组件的使用/AsyncAddress.vue @@ -0,0 +1,27 @@ + + + diff --git a/Vue/code/23.Suspense组件的使用/AsyncComponent.vue b/Vue/code/23.Suspense组件的使用/AsyncComponent.vue new file mode 100644 index 00000000..597683e0 --- /dev/null +++ b/Vue/code/23.Suspense组件的使用/AsyncComponent.vue @@ -0,0 +1,22 @@ + + + diff --git a/Vue/code/vue-project/.gitignore b/Vue/code/vue-project/.gitignore new file mode 100644 index 00000000..8ee54e8d --- /dev/null +++ b/Vue/code/vue-project/.gitignore @@ -0,0 +1,30 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +.DS_Store +dist +dist-ssr +coverage +*.local + +/cypress/videos/ +/cypress/screenshots/ + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? + +*.tsbuildinfo diff --git a/Vue/code/vue-project/README.md b/Vue/code/vue-project/README.md new file mode 100644 index 00000000..2df698b1 --- /dev/null +++ b/Vue/code/vue-project/README.md @@ -0,0 +1,40 @@ +# vue-project + +This template should help get you started developing with Vue 3 in Vite. + +## Recommended IDE Setup + +[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin). + +## Type Support for `.vue` Imports in TS + +TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types. + +If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps: + +1. Disable the built-in TypeScript Extension + 1) Run `Extensions: Show Built-in Extensions` from VSCode's command palette + 2) Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)` +2. Reload the VSCode window by running `Developer: Reload Window` from the command palette. + +## Customize configuration + +See [Vite Configuration Reference](https://vitejs.dev/config/). + +## Project Setup + +```sh +npm install +``` + +### Compile and Hot-Reload for Development + +```sh +npm run dev +``` + +### Type-Check, Compile and Minify for Production + +```sh +npm run build +``` diff --git a/Vue/code/vue-project/env.d.ts b/Vue/code/vue-project/env.d.ts new file mode 100644 index 00000000..11f02fe2 --- /dev/null +++ b/Vue/code/vue-project/env.d.ts @@ -0,0 +1 @@ +/// diff --git a/Vue/code/vue-project/index.html b/Vue/code/vue-project/index.html new file mode 100644 index 00000000..a8885448 --- /dev/null +++ b/Vue/code/vue-project/index.html @@ -0,0 +1,13 @@ + + + + + + + Vite App + + +
+ + + diff --git a/Vue/code/vue-project/package.json b/Vue/code/vue-project/package.json new file mode 100644 index 00000000..27c0620d --- /dev/null +++ b/Vue/code/vue-project/package.json @@ -0,0 +1,26 @@ +{ + "name": "vue-project", + "version": "0.0.0", + "private": true, + "type": "module", + "scripts": { + "dev": "vite", + "build": "run-p type-check \"build-only {@}\" --", + "preview": "vite preview", + "build-only": "vite build", + "type-check": "vue-tsc --build --force" + }, + "dependencies": { + "vue": "^3.3.11" + }, + "devDependencies": { + "@tsconfig/node18": "^18.2.2", + "@types/node": "^18.19.3", + "@vitejs/plugin-vue": "^4.5.2", + "@vue/tsconfig": "^0.5.0", + "npm-run-all2": "^6.1.1", + "typescript": "~5.3.0", + "vite": "^5.0.10", + "vue-tsc": "^1.8.25" + } +} diff --git a/Vue/code/vue-project/src/App.vue b/Vue/code/vue-project/src/App.vue new file mode 100644 index 00000000..864c5f0a --- /dev/null +++ b/Vue/code/vue-project/src/App.vue @@ -0,0 +1,24 @@ + + + + + diff --git a/Vue/code/vue-project/src/components/Person.vue b/Vue/code/vue-project/src/components/Person.vue new file mode 100644 index 00000000..f9bf077b --- /dev/null +++ b/Vue/code/vue-project/src/components/Person.vue @@ -0,0 +1,132 @@ + + + + + + + \ No newline at end of file diff --git a/Vue/code/vue-project/src/main.ts b/Vue/code/vue-project/src/main.ts new file mode 100644 index 00000000..ebc637ff --- /dev/null +++ b/Vue/code/vue-project/src/main.ts @@ -0,0 +1,5 @@ + +import { createApp } from 'vue' +import App from './App.vue' + +createApp(App).mount('#app') diff --git a/Vue/code/vue-project/tsconfig.app.json b/Vue/code/vue-project/tsconfig.app.json new file mode 100644 index 00000000..491e0939 --- /dev/null +++ b/Vue/code/vue-project/tsconfig.app.json @@ -0,0 +1,13 @@ +{ + "extends": "@vue/tsconfig/tsconfig.dom.json", + "include": ["env.d.ts", "src/**/*", "src/**/*.vue"], + "exclude": ["src/**/__tests__/*"], + "compilerOptions": { + "composite": true, + "noEmit": true, + "baseUrl": ".", + "paths": { + "@/*": ["./src/*"] + } + } +} diff --git a/Vue/code/vue-project/tsconfig.json b/Vue/code/vue-project/tsconfig.json new file mode 100644 index 00000000..66b5e570 --- /dev/null +++ b/Vue/code/vue-project/tsconfig.json @@ -0,0 +1,11 @@ +{ + "files": [], + "references": [ + { + "path": "./tsconfig.node.json" + }, + { + "path": "./tsconfig.app.json" + } + ] +} diff --git a/Vue/code/vue-project/tsconfig.node.json b/Vue/code/vue-project/tsconfig.node.json new file mode 100644 index 00000000..46cf2e14 --- /dev/null +++ b/Vue/code/vue-project/tsconfig.node.json @@ -0,0 +1,17 @@ +{ + "extends": "@tsconfig/node18/tsconfig.json", + "include": [ + "vite.config.*", + "vitest.config.*", + "cypress.config.*", + "nightwatch.conf.*", + "playwright.config.*" + ], + "compilerOptions": { + "composite": true, + "noEmit": true, + "module": "ESNext", + "moduleResolution": "Bundler", + "types": ["node"] + } +} diff --git a/Vue/code/vue-project/vite.config.ts b/Vue/code/vue-project/vite.config.ts new file mode 100644 index 00000000..5c45e1d9 --- /dev/null +++ b/Vue/code/vue-project/vite.config.ts @@ -0,0 +1,16 @@ +import { fileURLToPath, URL } from 'node:url' + +import { defineConfig } from 'vite' +import vue from '@vitejs/plugin-vue' + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [ + vue(), + ], + resolve: { + alias: { + '@': fileURLToPath(new URL('./src', import.meta.url)) + } + } +}) diff --git a/Vue/01-ES6补充.md b/Vue/vue2/01-ES6补充.md similarity index 100% rename from Vue/01-ES6补充.md rename to Vue/vue2/01-ES6补充.md diff --git a/Vue/02-HelloVue.md b/Vue/vue2/02-HelloVue.md similarity index 100% rename from Vue/02-HelloVue.md rename to Vue/vue2/02-HelloVue.md diff --git a/Vue/03-插值操作.md b/Vue/vue2/03-插值操作.md similarity index 100% rename from Vue/03-插值操作.md rename to Vue/vue2/03-插值操作.md diff --git a/Vue/04-动态绑定属性.md b/Vue/vue2/04-动态绑定属性.md similarity index 100% rename from Vue/04-动态绑定属性.md rename to Vue/vue2/04-动态绑定属性.md diff --git a/Vue/05-计算属性与侦听器.md b/Vue/vue2/05-计算属性与侦听器.md similarity index 100% rename from Vue/05-计算属性与侦听器.md rename to Vue/vue2/05-计算属性与侦听器.md diff --git a/Vue/06-事件监听.md b/Vue/vue2/06-事件监听.md similarity index 100% rename from Vue/06-事件监听.md rename to Vue/vue2/06-事件监听.md diff --git a/Vue/07-条件判断.md b/Vue/vue2/07-条件判断.md similarity index 100% rename from Vue/07-条件判断.md rename to Vue/vue2/07-条件判断.md diff --git a/Vue/08-循环遍历.md b/Vue/vue2/08-循环遍历.md similarity index 100% rename from Vue/08-循环遍历.md rename to Vue/vue2/08-循环遍历.md diff --git a/Vue/09-综合练习.md b/Vue/vue2/09-综合练习.md similarity index 100% rename from Vue/09-综合练习.md rename to Vue/vue2/09-综合练习.md diff --git a/Vue/10-v-model.md b/Vue/vue2/10-v-model.md similarity index 100% rename from Vue/10-v-model.md rename to Vue/vue2/10-v-model.md diff --git a/Vue/11-组件化开发.md b/Vue/vue2/11-组件化开发.md similarity index 100% rename from Vue/11-组件化开发.md rename to Vue/vue2/11-组件化开发.md diff --git a/Vue/12-组件化高级.md b/Vue/vue2/12-组件化高级.md similarity index 100% rename from Vue/12-组件化高级.md rename to Vue/vue2/12-组件化高级.md diff --git a/Vue/13-Vue实例的生命周期.md b/Vue/vue2/13-Vue实例的生命周期.md similarity index 100% rename from Vue/13-Vue实例的生命周期.md rename to Vue/vue2/13-Vue实例的生命周期.md diff --git a/Vue/14-前端模块化.md b/Vue/vue2/14-前端模块化.md similarity index 100% rename from Vue/14-前端模块化.md rename to Vue/vue2/14-前端模块化.md diff --git a/Vue/15-webpack.md b/Vue/vue2/15-webpack.md similarity index 100% rename from Vue/15-webpack.md rename to Vue/vue2/15-webpack.md diff --git a/Vue/16-VueCLI.md b/Vue/vue2/16-VueCLI.md similarity index 100% rename from Vue/16-VueCLI.md rename to Vue/vue2/16-VueCLI.md diff --git a/Vue/17-Vue-Router.md b/Vue/vue2/17-Vue-Router.md similarity index 100% rename from Vue/17-Vue-Router.md rename to Vue/vue2/17-Vue-Router.md diff --git a/Vue/18-Promise.md b/Vue/vue2/18-Promise.md similarity index 100% rename from Vue/18-Promise.md rename to Vue/vue2/18-Promise.md diff --git a/Vue/19-Vuex.md b/Vue/vue2/19-Vuex.md similarity index 100% rename from Vue/19-Vuex.md rename to Vue/vue2/19-Vuex.md diff --git a/Vue/20-Axios的封装.md b/Vue/vue2/20-Axios的封装.md similarity index 100% rename from Vue/20-Axios的封装.md rename to Vue/vue2/20-Axios的封装.md diff --git a/Vue/README.md b/Vue/vue2/README.md similarity index 100% rename from Vue/README.md rename to Vue/vue2/README.md diff --git a/Vue/综合练习-实现Tab-Bar.md b/Vue/vue2/综合练习-实现Tab-Bar.md similarity index 100% rename from Vue/综合练习-实现Tab-Bar.md rename to Vue/vue2/综合练习-实现Tab-Bar.md diff --git a/Vue/vue3/vue3快速上手.md b/Vue/vue3/vue3快速上手.md new file mode 100644 index 00000000..34870efa --- /dev/null +++ b/Vue/vue3/vue3快速上手.md @@ -0,0 +1,616 @@ +# Vue3快速上手 + + +## 1.Vue3带来了什么 + +### 1.性能的提升 + +- 打包大小减少41% + +- 初次渲染快55%, 更新渲染快133% + +- 内存减少54% + + ...... + +### 2.源码的升级 + +- 使用Proxy代替defineProperty实现响应式 + +- 重写虚拟DOM的实现和Tree-Shaking + + ...... + +### 3.拥抱TypeScript + +- Vue3可以更好的支持TypeScript + +### 4.新的特性 + +1. Composition API(组合API) + + - setup配置 + - ref与reactive + - watch与watchEffect + - provide与inject + - ...... +2. 新的内置组件 + - Fragment + - Teleport + - Suspense +3. 其他改变 + + - 新的生命周期钩子 + - data 选项应始终被声明为一个函数 + - 移除keyCode支持作为 v-on 的修饰符 + - ...... + +# 一、创建Vue3.0工程 + +## 1.使用 vue-cli 创建 + +官方文档:https://cli.vuejs.org/zh/guide/creating-a-project.html#vue-create + +```bash +## 查看@vue/cli版本,确保@vue/cli版本在4.5.0以上 +vue --version +## 安装或者升级你的@vue/cli +npm install -g @vue/cli +## 创建 +vue create vue_test +## 启动 +cd vue_test +npm run serve +``` + +## 2.使用 vite 创建 + +官方文档:https://v3.cn.vuejs.org/guide/installation.html#vite + +vite官网:https://vitejs.cn + +- 什么是vite?—— 新一代前端构建工具。 +- 优势如下: + - 开发环境中,无需打包操作,可快速的冷启动。 + - 轻量快速的热重载(HMR)。 + - 真正的按需编译,不再等待整个应用编译完成。 +- 传统构建 与 vite构建对比图 + + +```bash +## 创建工程 +npm init vite-app +## 进入工程目录 +cd +## 安装依赖 +npm install +## 运行 +npm run dev +``` + +# 二、常用 Composition API + +官方文档: https://v3.cn.vuejs.org/guide/composition-api-introduction.html + +## 1.拉开序幕的setup + +1. 理解:Vue3.0中一个新的配置项,值为一个函数。 +2. setup是所有Composition API(组合API)“ 表演的舞台 ”。 +4. 组件中所用到的:数据、方法等等,均要配置在setup中。 +5. setup函数的两种返回值: + 1. 若返回一个对象,则对象中的属性、方法, 在模板中均可以直接使用。(重点关注!) + 2. 若返回一个渲染函数:则可以自定义渲染内容。(了解) +6. 注意点: + 1. 尽量不要与Vue2.x配置混用 + - Vue2.x配置(data、methos、computed...)中可以访问到setup中的属性、方法。 + - 但在setup中不能访问到Vue2.x配置(data、methos、computed...)。 + - 如果有重名, setup优先。 + 2. setup不能是一个async函数,因为返回值不再是return的对象, 而是promise, 模板看不到return对象中的属性。(后期也可以返回一个Promise实例,但需要Suspense和异步组件的配合) + +## 2.ref函数 + +- 作用: 定义一个响应式的数据 +- 语法: ```const xxx = ref(initValue)``` + - 创建一个包含响应式数据的引用对象(reference对象,简称ref对象)。 + - JS中操作数据: ```xxx.value``` + - 模板中读取数据: 不需要.value,直接:```
{{xxx}}
``` +- 备注: + - 接收的数据可以是:基本类型、也可以是对象类型。 + - 基本类型的数据:响应式依然是靠``Object.defineProperty()``的```get```与```set```完成的。 + - 对象类型的数据:内部 “ 求助 ” 了Vue3.0中的一个新函数—— ```reactive```函数。 + +## 3.reactive函数 + +- 作用: 定义一个对象类型的响应式数据(基本类型不要用它,要用```ref```函数) +- 语法:```const 代理对象= reactive(源对象)```接收一个对象(或数组),返回一个代理对象(Proxy的实例对象,简称proxy对象) +- reactive定义的响应式数据是“深层次的”。 +- 内部基于 ES6 的 Proxy 实现,通过代理对象操作源对象内部数据进行操作。 + +## 4.Vue3.0中的响应式原理 + +### vue2.x的响应式 + +- 实现原理: + - 对象类型:通过```Object.defineProperty()```对属性的读取、修改进行拦截(数据劫持)。 + + - 数组类型:通过重写更新数组的一系列方法来实现拦截。(对数组的变更方法进行了包裹)。 + + ```js + Object.defineProperty(data, 'count', { + get () {}, + set () {} + }) + ``` + +- 存在问题: + - 新增属性、删除属性, 界面不会更新。 + - 直接通过下标修改数组, 界面不会自动更新。 + +### Vue3.0的响应式 + +- 实现原理: + - 通过Proxy(代理): 拦截对象中任意属性的变化, 包括:属性值的读写、属性的添加、属性的删除等。 + - 通过Reflect(反射): 对源对象的属性进行操作。 + - MDN文档中描述的Proxy与Reflect: + - Proxy:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy + + - Reflect:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Reflect + + ```js + new Proxy(data, { + // 拦截读取属性值 + get (target, prop) { + return Reflect.get(target, prop) + }, + // 拦截设置属性值或添加新属性 + set (target, prop, value) { + return Reflect.set(target, prop, value) + }, + // 拦截删除属性 + deleteProperty (target, prop) { + return Reflect.deleteProperty(target, prop) + } + }) + + proxy.name = 'tom' + ``` + +## 5.reactive对比ref + +- 从定义数据角度对比: + - ref用来定义:基本类型数据。 + - reactive用来定义:对象(或数组)类型数据。 + - 备注:ref也可以用来定义对象(或数组)类型数据, 它内部会自动通过```reactive```转为代理对象。 +- 从原理角度对比: + - ref通过``Object.defineProperty()``的```get```与```set```来实现响应式(数据劫持)。 + - reactive通过使用Proxy来实现响应式(数据劫持), 并通过Reflect操作源对象内部的数据。 +- 从使用角度对比: + - ref定义的数据:操作数据需要```.value```,读取数据时模板中直接读取不需要```.value```。 + - reactive定义的数据:操作数据与读取数据:均不需要```.value```。 + +## 6.setup的两个注意点 + +- setup执行的时机 + - 在beforeCreate之前执行一次,this是undefined。 + +- setup的参数 + - props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。 + - context:上下文对象 + - attrs: 值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性, 相当于 ```this.$attrs```。 + - slots: 收到的插槽内容, 相当于 ```this.$slots```。 + - emit: 分发自定义事件的函数, 相当于 ```this.$emit```。 + + +## 7.计算属性与监视 + +### 1.computed函数 + +- 与Vue2.x中computed配置功能一致 + +- 写法 + + ```js + import {computed} from 'vue' + + setup(){ + ... + //计算属性——简写 + let fullName = computed(()=>{ + return person.firstName + '-' + person.lastName + }) + //计算属性——完整 + let fullName = computed({ + get(){ + return person.firstName + '-' + person.lastName + }, + set(value){ + const nameArr = value.split('-') + person.firstName = nameArr[0] + person.lastName = nameArr[1] + } + }) + } + ``` + +### 2.watch函数 + +- 与Vue2.x中watch配置功能一致 + +- 两个小“坑”: + + - 监视reactive定义的响应式数据时:oldValue无法正确获取、强制开启了深度监视(deep配置失效)。 + - 监视reactive定义的响应式数据中某个属性时:deep配置有效。 + + ```js + //情况一:监视ref定义的响应式数据 + watch(sum,(newValue,oldValue)=>{ + console.log('sum变化了',newValue,oldValue) + },{immediate:true}) + + //情况二:监视多个ref定义的响应式数据 + watch([sum,msg],(newValue,oldValue)=>{ + console.log('sum或msg变化了',newValue,oldValue) + }) + + /* 情况三:监视reactive定义的响应式数据 + 若watch监视的是reactive定义的响应式数据,则无法正确获得oldValue!! + 若watch监视的是reactive定义的响应式数据,则强制开启了深度监视 + */ + watch(person,(newValue,oldValue)=>{ + console.log('person变化了',newValue,oldValue) + },{immediate:true,deep:false}) //此处的deep配置不再奏效 + + //情况四:监视reactive定义的响应式数据中的某个属性 + watch(()=>person.job,(newValue,oldValue)=>{ + console.log('person的job变化了',newValue,oldValue) + },{immediate:true,deep:true}) + + //情况五:监视reactive定义的响应式数据中的某些属性 + watch([()=>person.job,()=>person.name],(newValue,oldValue)=>{ + console.log('person的job变化了',newValue,oldValue) + },{immediate:true,deep:true}) + + //特殊情况 + watch(()=>person.job,(newValue,oldValue)=>{ + console.log('person的job变化了',newValue,oldValue) + },{deep:true}) //此处由于监视的是reactive素定义的对象中的某个属性,所以deep配置有效 + ``` + +### 3.watchEffect函数 + +- watch的套路是:既要指明监视的属性,也要指明监视的回调。 + +- watchEffect的套路是:不用指明监视哪个属性,监视的回调中用到哪个属性,那就监视哪个属性。 + +- watchEffect有点像computed: + + - 但computed注重的计算出来的值(回调函数的返回值),所以必须要写返回值。 + - 而watchEffect更注重的是过程(回调函数的函数体),所以不用写返回值。 + + ```js + //watchEffect所指定的回调中用到的数据只要发生变化,则直接重新执行回调。 + watchEffect(()=>{ + const x1 = sum.value + const x2 = person.age + console.log('watchEffect配置的回调执行了') + }) + ``` + +## 8.生命周期 + +- Vue3.0中可以继续使用Vue2.x中的生命周期钩子,但有有两个被更名: + - ```beforeDestroy```改名为 ```beforeUnmount``` + - ```destroyed```改名为 ```unmounted``` +- Vue3.0也提供了 Composition API 形式的生命周期钩子,与Vue2.x中钩子对应关系如下: + - `beforeCreate`===>`setup()` + - `created`=======>`setup()` + - `beforeMount` ===>`onBeforeMount` + - `mounted`=======>`onMounted` + - `beforeUpdate`===>`onBeforeUpdate` + - `updated` =======>`onUpdated` + - `beforeUnmount` ==>`onBeforeUnmount` + - `unmounted` =====>`onUnmounted` + +## 9.自定义hook函数 + +- 什么是hook?—— 本质是一个函数,把setup函数中使用的Composition API进行了封装。 + +- 类似于vue2.x中的mixin。 + +- 自定义hook的优势: 复用代码, 让setup中的逻辑更清楚易懂。 + + + +## 10.toRef + +- 作用:创建一个 ref 对象,其value值指向另一个对象中的某个属性。 +- 语法:```const name = toRef(person,'name')``` +- 应用: 要将响应式对象中的某个属性单独提供给外部使用时。 + + +- 扩展:```toRefs``` 与```toRef```功能一致,但可以批量创建多个 ref 对象,语法:```toRefs(person)``` + + +# 三、其它 Composition API + +## 1.shallowReactive 与 shallowRef + +- shallowReactive:只处理对象最外层属性的响应式(浅响应式)。 +- shallowRef:只处理基本数据类型的响应式, 不进行对象的响应式处理。 + +- 什么时候使用? + - 如果有一个对象数据,结构比较深, 但变化时只是外层属性变化 ===> shallowReactive。 + - 如果有一个对象数据,后续功能不会修改该对象中的属性,而是生新的对象来替换 ===> shallowRef。 + +## 2.readonly 与 shallowReadonly + +- readonly: 让一个响应式数据变为只读的(深只读)。 +- shallowReadonly:让一个响应式数据变为只读的(浅只读)。 +- 应用场景: 不希望数据被修改时。 + +## 3.toRaw 与 markRaw + +- toRaw: + - 作用:将一个由```reactive```生成的响应式对象转为普通对象。 + - 使用场景:用于读取响应式对象对应的普通对象,对这个普通对象的所有操作,不会引起页面更新。 +- markRaw: + - 作用:标记一个对象,使其永远不会再成为响应式对象。 + - 应用场景: + 1. 有些值不应被设置为响应式的,例如复杂的第三方类库等。 + 2. 当渲染具有不可变数据源的大列表时,跳过响应式转换可以提高性能。 + +## 4.customRef + +- 作用:创建一个自定义的 ref,并对其依赖项跟踪和更新触发进行显式控制。 + +- 实现防抖效果: + + ```vue + + + + ``` + + + +## 5.provide 与 inject + + + +- 作用:实现祖与后代组件间通信 + +- 套路:父组件有一个 `provide` 选项来提供数据,后代组件有一个 `inject` 选项来开始使用这些数据 + +- 具体写法: + + 1. 祖组件中: + + ```js + setup(){ + ...... + let car = reactive({name:'奔驰',price:'40万'}) + provide('car',car) + ...... + } + ``` + + 2. 后代组件中: + + ```js + setup(props,context){ + ...... + const car = inject('car') + return {car} + ...... + } + ``` + +## 6.响应式数据的判断 + +- isRef: 检查一个值是否为一个 ref 对象 +- isReactive: 检查一个对象是否是由 `reactive` 创建的响应式代理 +- isReadonly: 检查一个对象是否是由 `readonly` 创建的只读代理 +- isProxy: 检查一个对象是否是由 `reactive` 或者 `readonly` 方法创建的代理 + +# 四、Composition API 的优势 + +## 1.Options API 存在的问题 + +使用传统OptionsAPI中,新增或者修改一个需求,就需要分别在data,methods,computed里修改 。 + + + +## 2.Composition API 的优势 + +我们可以更加优雅的组织我们的代码,函数。让相关功能的代码更加有序的组织在一起。 + + + + +# 五、新的组件 + +## 1.Fragment + +- 在Vue2中: 组件必须有一个根标签 +- 在Vue3中: 组件可以没有根标签, 内部会将多个标签包含在一个Fragment虚拟元素中 +- 好处: 减少标签层级, 减小内存占用 + +## 2.Teleport + +- 什么是Teleport?—— `Teleport` 是一种能够将我们的组件html结构移动到指定位置的技术。 + + ```vue + +
+
+

我是一个弹窗

+ +
+
+
+ ``` + +## 3.Suspense + +- 等待异步组件时渲染一些额外内容,让应用有更好的用户体验 + +- 使用步骤: + + - 异步引入组件 + + ```js + import {defineAsyncComponent} from 'vue' + const Child = defineAsyncComponent(()=>import('./components/Child.vue')) + ``` + + - 使用```Suspense```包裹组件,并配置好```default``` 与 ```fallback``` + + ```vue + + ``` + +# 六、其他 + +## 1.全局API的转移 + +- Vue 2.x 有许多全局 API 和配置。 + - 例如:注册全局组件、注册全局指令等。 + + ```js + //注册全局组件 + Vue.component('MyButton', { + data: () => ({ + count: 0 + }), + template: '' + }) + + //注册全局指令 + Vue.directive('focus', { + inserted: el => el.focus() + } + ``` + +- Vue3.0中对这些API做出了调整: + + - 将全局的API,即:```Vue.xxx```调整到应用实例(```app```)上 + + | 2.x 全局 API(```Vue```) | 3.x 实例 API (`app`) | + | ------------------------- | ------------------------------------------- | + | Vue.config.xxxx | app.config.xxxx | + | Vue.config.productionTip | 移除 | + | Vue.component | app.component | + | Vue.directive | app.directive | + | Vue.mixin | app.mixin | + | Vue.use | app.use | + | Vue.prototype | app.config.globalProperties | + + +## 2.其他改变 + +- data选项应始终被声明为一个函数。 + +- 过度类名的更改: + + - Vue2.x写法 + + ```css + .v-enter, + .v-leave-to { + opacity: 0; + } + .v-leave, + .v-enter-to { + opacity: 1; + } + ``` + + - Vue3.x写法 + + ```css + .v-enter-from, + .v-leave-to { + opacity: 0; + } + + .v-leave-from, + .v-enter-to { + opacity: 1; + } + ``` + +- 移除keyCode作为 v-on 的修饰符,同时也不再支持```config.keyCodes``` + +- 移除```v-on.native```修饰符 + + - 父组件中绑定事件 + + ```vue + + ``` + + - 子组件中声明自定义事件 + + ```vue + + ``` + +- 移除过滤器(filter) + + > 过滤器虽然这看起来很方便,但它需要一个自定义语法,打破大括号内表达式是 “只是 JavaScript” 的假设,这不仅有学习成本,而且有实现成本!建议用方法调用或计算属性去替换过滤器。 + +- ...... \ No newline at end of file