vue3相关文档
- 安装vue3项目
- Vue Router
- Vue cli
- blog
- Vue3 api文档
- 从vue2迁徙
- vue源码地址
- vue-router源码地址
- vue-cli源码地址
- vuex源码地址
- devtools源码地址
开源项目
vue-pure-admin (opens new window)是使用vue3 vite2 Element-Plus TypeScript
- vue-pure-admin
- 项目源码GitHub地址
- 项目源码码云地址
- 项目学习地址 vue3-admin 是使用了Vue3 + vite + element-plus的后台管理系统
- vue3-admin
展示一下官网api,以及使用
-
- errorHandler
- warnHandler
- globalProperties
- optionMergeStrategies
- performance
- compilerOptions 3.1+
-
- component
- config
- directive
- mixin
- mount
- provide
- unmount
- use
- version
-
- createApp
- h
- defineComponent
- defineAsyncComponent
- defineCustomElement
- resolveDynamicComponent
- resolveDirective
- withDirectives
- createRenderer
- nextTick
- mergeProps
- useCssModule
- ersion
-
- Data
- data
- props
- computed
- methods
- watch
- emits
- expose
- DOM
- template
- render
- 生命周期钩子
- beforeCreate
- created
- beforeMount
- mounted
- beforeUpdate
- updated
- activated
- deactivated
- beforeUnmount
- unmounted
- errorCaptured
- renderTracked
- renderTriggered
- 选项/资源
- directives
- components
- 组合
- mixins
- extends
- provide inject
- setup
- 杂项
- name
- inheritAttrs
- compilerOptions
- delimiters
- Data
-
- $data
- $props
- $el
- $options
- $parent
- $root
- $slots
- $refs
- $attrs
-
- $watch
- $emit
- $forceUpdate
- $nextTick
-
- v-text
- v-html
- v-show
- v-if
- v-else
- v-else-if
- v-for
- v-on
- v-bind
- v-model
- v-slot
- v-pre
- v- cloak
- v-once
- v-memo
- v-is
-
- key
- ref
- is
-
- component
- transition
- transition-group
- keep-alive
- slot
- teleport
-
- 响应性基础 API
- Refs
- Computed 与 watch
- Effect 作用域 API
-
- setup
- 生命周期钩子
- Provide / Inject
- getCurrentInstance
-
单文件组件
- 规范
- 工具
<script setup>- 特性
api使用方法
<body>
<div id="app">
<button @click="add">afdas</button>
{{count}} {{doubleCount}}
</div>
<script>
const { reactive, createApp, computed, watch, onMounted, ref , toRefs } = Vue
createApp({
setup() {
// 在这里写代码
}
}).mount('#app')
</script>
</body>
globalProperties
app.config.globalProperties.foo =
getCurrentInstance().proxy.foo // 'bar'
ref
<template>
<div>{{a}}</div> <div>{{b}}</div>
</template>
import { ref } from '@vue/runtime-core'
setup(props){
let a = ref(0);{ _isRef:true,value:1 }
let b = ref(0);
console.log(a.value,b.value)
return {
a,
b
}
}
<template>
<div>{{obj.a}}</div> <div>{{obj.b}}</div>
</template>
import { ref } from '@vue/runtime-core'
setup(props){
let obj = ref({
a:0,
b:0
})
console.log(obj.a.value , obj.b.value)
return {
obj
}
}
unref
let supNum= ref(0)
console.log(unref(supNum)) // 0
val = isRef(val) ? val.value : val
toRefs
<template>
<div>{{a}}</div> <div>{{b}}</div>
</template>
import { reactive , toRefs } from '@vue/runtime-core'
setup(props){
let obj = reactive({
a:10,
b:20
})
obj.a = 1
obj.b = 2
return {
...toRefs(obj) //一个,则用toRef(obj, 'foo')
}
}
reactive
<template>
<div>{{state.supNum}}</div> <div>{{state.oppNum}}</div>
</template>
import { reactive } from '@vue/runtime-core'
setup(props){
let state = reactive({
supNum:0,
oppNum:0
})
function change(lx){
lx === 0 ? state.supNum++ : state.oppNum++;
}
return {
state,
change
}
}
toRaw
const foo = {}
const reactiveFoo = reactive(foo)
toRaw(reactiveFoo) === foo // true
readonly
readonly(
reactive({ count: 0 })
).count ++ // 变更副本将失败并导致警告
isProxy
isProxy(reactive({ count: 0 })) //true
isReactive
isReactive(reactive({ count: 0 })) //true
isReadonly
isReadonly(readonly(reactive({ count: 0 }))) // true
computed
// computed值不可以直接修改,否则报错
const plusOne = computed(()=>ref(0).value + 1)
plusOne.value // 2
plusOne++
// 修改computed值方案
const count = ref(1);
const plusOne = computed({
get:()=>count.value + 2,
set:val => {count.value = val - 1 }
})
plusOne.value = 1 ;// 设置
console.log(plusOne.value);// 2
生命周期函数
beforeCreate setup
created setup
beforeMount onBeforeMount
mounted onMounted
beforeUpdate onBeforeUpdate
updated onUpdated
beforeDestroy onBeforeUnmount
destroyed onUnmounted
errorCaptured onErrorCaptured
定义组件
import { h, defineComponent } from "vue";
var IconifyIconOffline = defineComponent({
name: "IconifyIcon",
components: { IconifyIcon },//这是另外一个组件
props: {
icon: {
type: String,
default: ""
}
},
render() {
const attrs = this.$attrs;
return h(
IconifyIcon,
{
icon: `${this.icon}`,
...attrs
},
{
default: () => []
}
);
}
});
const app = createApp(App);
app.component("IconifyIconOffline", IconifyIconOffline);
app.mount("#app");
安装的vscode插件
Ts
为什么 Vue 3 是用 TypeScript 编写的??? 静态类型系统可以帮助防止许多潜在的运行时错误
定义 Vue 组件 要让 TypeScript 正确推断 Vue 组件选项中的类型,需要使用 defineComponent 全局方法定义组件:
import { defineComponent } from 'vue'
const Component = defineComponent({
// 已启用类型推断
})
如果你使用的是单文件组件,则通常会被写成:
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
// 已启用类型推断
})
</script>
在写vue的ts代码时候,很明显代码提示变强了 与 Options API 一起使用 对一个数字 做split操作时候,ts立马检测出错误来
this.count.split('') // => Property 'split' does not exist on type 'number'
如果你有一个复杂的类型或接口,你可以使用 type assertion 对其进行指明
interface Book {
title: string
year: number
}
const Component = defineComponent({
data() {
return {
book: {
title: 'Vue 3 Guide',
year: 2020
} as Book
}
}
})
为 globalProperties 扩充类型
Vue 3 提供了一个 globalProperties 对象,用来添加可以被任意组件实例访问的全局 property。例如一个插件想要注入一个共享全局对象或函数。
// 用户定义
import axios from 'axios'
const app = Vue.createApp({})
app.config.globalProperties.$http = axios
// 验证数据的插件
export default {
install(app, options) {
app.config.globalProperties.$validate = (data: object, rule: object) => {
// 检查对象是否合规
}
}
}
为了告诉 TypeScript 这些新 property,我们可以使用模块扩充 (module augmentation)。
在上述示例中,我们可以添加以下类型声明:
import axios from 'axios'
declare module '@vue/runtime-core' {
export interface ComponentCustomProperties {
$http: typeof axios
$validate: (data: object, rule: object) => boolean
}
}
我们可以把这些类型声明放在同一个文件里,或一个项目级别的 *.d.ts 文件 (例如在 TypeScript 会自动加载的 src/typings 文件夹中)。对于库/插件作者来说,这个文件应该被定义在 package.json 的 types property 里。
//node_modules/vue-router/package.json
{
"types": "dist/vue-router.d.ts",
}
"types": "dist/vue-router.d.ts",
当命令行上指定了输入文件时,tsconfig.json文件会被忽略。 在声明文件tsconfig.json的exclude和include或者使用files字段下指定要检测的文件路径; files 只能使用相对路径 或者绝对路径
include/exclude 可以使用模糊匹配的字符代替 某一特征名的文件 点击下方链接查看说明
tsconfig.json文件可以是个空文件,那么所有默认的文件都会以默认配置选项编译。
默认所有可见的"@types"包会在编译过程中被包含进来,包括node_modules 内所有的
如果指定了typeRoots,只有typeRoots下面的包才会被包含进来
{
"compilerOptions": {
"typeRoots" : ["./typings"]
}
}
如果指定了types,只有被列出来的包才会被包含进来
这个tsconfig.json文件将仅会包含 ./node_modules/@types/node,./node_modules/@types/lodash和./node_modules/@types/express
{
"compilerOptions": {
"types" : ["node", "lodash", "express"]
}
}
默认所有可见的"@types"包会在编译过程中被包含进来 简单说就是 这样./node_modules/@types/,../node_modules/@types/和../../node_modules/@types/ 指定"types": [ ]来禁用自动引入@types包
可以继承别的包属性
"extends": "./configs/base",
vite
基于浏览器原生ES imports的开发服务器,利用浏览器去解析imports,在服务器端按需编译返回,完全 跳过了打包这个概念,服务器随其随用 同时不仅有vue文件支持,还搞定了热更新,而且热更新的速度不会随着模块增加而变慢
使用vite创建vue项目 create-vite-app包
npm init @vitejs/app <project-name>//可以选择创建vue项目 也可以创建react项目
cd <project-name>
npm install
npm run dev