国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片

Vue 3 備忘清單

NPM version Downloads Repo Dependents Github repo

漸進(jìn)式 JavaScript 框架 Vue 3 備忘清單的快速參考列表,包含常用 API 和示例

入門(mén)

介紹

Vue 是一套用于構(gòu)建用戶界面的漸進(jìn)式框架

注意:Vue 3.x 版本對(duì)應(yīng) Vue Router 4.x 路由版本

創(chuàng)建應(yīng)用

已安裝 16.0 或更高版本的 Node.js

$ npm init vue@latest

指令將會(huì)安裝并執(zhí)行 create-vue,它是 Vue 官方的項(xiàng)目腳手架工具

? Project name: … <your-project-name>
? Add TypeScript? … No/Yes
? Add JSX Support? … No/Yes
? Add Vue Router for Single Page Application development? … No/Yes
? Add Pinia for state management? … No/Yes
? Add Vitest for Unit testing? … No/Yes
? Add Cypress for both Unit and End-to-End testing? … No/Yes
? Add ESLint for code quality? … No/Yes
? Add Prettier for code formatting? … No/Yes

Scaffolding project in ./<your-project-name>...
Done.

安裝依賴并啟動(dòng)開(kāi)發(fā)服務(wù)器

$ cd <your-project-name>
$ npm install
$ npm run dev

當(dāng)你準(zhǔn)備將應(yīng)用發(fā)布到生產(chǎn)環(huán)境時(shí),請(qǐng)運(yùn)行:

$ npm run build

此命令會(huì)在 ./dist 文件夾中為你的應(yīng)用創(chuàng)建一個(gè)生產(chǎn)環(huán)境的構(gòu)建版本

應(yīng)用實(shí)例

import { createApp, ref } from 'vue'

const app = createApp({
  setup() {
    const message = ref("Hello Vue3")
    return {
      message
    }
  }
})
app.mount('#app')

掛載應(yīng)用

<div id="app">
  <button @click="count++">
    {{ count }}
  </button>
</div>

通過(guò) CDN 使用 Vue

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">{{ message }}</div>
<script>
  const { createApp, ref } = Vue
  createApp({
    setup() {
      const message = ref("Hello Vue3")
      return {
        message
      }
    }
  }).mount('#app')
</script>

使用 ES 模塊構(gòu)建版本

<div id="app">{{ message, ref }}</div>
<script type="module">
  import { createApp, ref } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  createApp({
    setup() {
      const message = ref("Hello Vue3")
      return {
        message
      }
    }
  }).mount('#app')
</script>

模板語(yǔ)法

文本插值

<span>Message: {{ msg }}</span>

使用的是 Mustache 語(yǔ)法 (即雙大括號(hào)),每次 msg 屬性更改時(shí)它也會(huì)同步更新

原始 HTML

<p>Using text interpolation: {{ rawHtml }}</p>
<p>Using v-html directive: <span v-html="rawHtml"></span></p>

雙大括號(hào){{}}會(huì)將數(shù)據(jù)解釋為純文本,使用 v-html 指令,將插入 HTML

Attribute 綁定

<div v-bind:id="dynamicId"></div>

簡(jiǎn)寫(xiě)

<div :id="dynamicId"></div>

布爾型 Attribute

<button :disabled="isButtonDisabled">
  Button
</button>

動(dòng)態(tài)綁定多個(gè)值

通過(guò)不帶參數(shù)的 v-bind,你可以將它們綁定到單個(gè)元素上

<script setup>
  import comp from "./Comp.vue"
  import {ref} from "vue"
  const a = ref("hello")
  const b = ref("world")
</script>

<template>
  <comp v-bind="{a, b}"></comp>
</template>

如果你是使用的 setup 語(yǔ)法糖。需要使用 defineprops 聲名(可以直接使用a/b

const props = defineProps({
  a: String,
  b: String
})

使用 JavaScript 表達(dá)式

{{ number + 1 }}
{{ ok ? 'YES' : 'NO' }}
{{ message.split('').reverse().join('') }}

<div :id="`list-${id}`"></div>

僅支持表達(dá)式(例子都是無(wú)效)

<!-- 這是一個(gè)語(yǔ)句,而非表達(dá)式 -->
{{ var a = 1 }}
<!-- 條件控制也不支持,請(qǐng)使用三元表達(dá)式 -->
{{ if (ok) { return message } }}

調(diào)用函數(shù)

<span :title="toTitleDate(date)">
  {{ formatDate(date) }}
</span>

指令 Directives

<p v-if="seen">Now you see me</p>

參數(shù) Arguments

<a v-bind:href="url"> ... </a>
<!-- 簡(jiǎn)寫(xiě) -->
<a :href="url"> ... </a>

綁定事件

<a v-on:click="doSomething"> ... </a>
<!-- 簡(jiǎn)寫(xiě) -->
<a @click="doSomething"> ... </a>

動(dòng)態(tài)參數(shù)

<a v-bind:[attributeName]="url"> ... </a>
<!-- 簡(jiǎn)寫(xiě) -->
<a :[attributeName]="url"> ... </a>

這里的 attributeName 會(huì)作為一個(gè) JS 表達(dá)式被動(dòng)態(tài)執(zhí)行

動(dòng)態(tài)的事件名稱(chēng)

<a v-on:[eventName]="doSomething"> ... </a>
<!-- 簡(jiǎn)寫(xiě) -->
<a @[eventName]="doSomething">

修飾符 Modifiers

<form @submit.prevent="onSubmit">
  ...
</form>

.prevent 修飾符會(huì)告知 v-on 指令對(duì)觸發(fā)的事件調(diào)用 event.preventDefault()

指令語(yǔ)法

v-on:submit.prevent="onSubmit"
──┬─ ─┬──── ─┬─────  ─┬──────
  ┆   ┆      ┆        ╰─ Value 解釋為JS表達(dá)式
  ┆   ┆      ╰─ Modifiers 由前導(dǎo)點(diǎn)表示
  ┆   ╰─ Argument 跟隨冒號(hào)或速記符號(hào)
  ╰─ Name 以 v- 開(kāi)頭使用速記時(shí)可以省略

響應(yīng)式基礎(chǔ)

聲明狀態(tài)

<div>{{ state.count }}</div>

import { defineComponent, reactive } from 'vue';

// `defineComponent`用于IDE推導(dǎo)類(lèi)型
export default defineComponent({
  // setup 用于組合式 API 的特殊鉤子函數(shù)
  setup() {
    const state = reactive({ count: 0 });

    // 暴露 state 到模板
    return {
      state
    };
  },
});

聲明方法

<button @click="increment">
  {{ state.count }}
</button>

import { defineComponent, reactive } from 'vue';

export default defineComponent({
  setup() {
    const state = reactive({ count: 0 });

    function increment() {
      state.count++;
    }

    // 不要忘記同時(shí)暴露 increment 函數(shù)
    return {
      state,
      increment
    };
  },
})

<script setup> setup語(yǔ)法糖

<script setup>
import { reactive } from 'vue';

const state = reactive({ count: 0 })

function increment() {
  state.count++
}
</script>

<template>
  <button @click="increment">
    {{ state.count }}
  </button>
</template>

setup 語(yǔ)法糖用于簡(jiǎn)化代碼,尤其是當(dāng)需要暴露的狀態(tài)和方法越來(lái)越多時(shí)

ref() 定義響應(yīng)式變量

reactive只能用于對(duì)象、數(shù)組和 Map、Set 這樣的集合類(lèi)型,對(duì) string、number 和 boolean 這樣的原始類(lèi)型則需要使用ref

import { ref } from 'vue';

const count = ref(0);

console.log(count); // { value: 0 }
console.log(count.value); // 0
count.value++;
console.log(count.value); // 1
const objectRef = ref({ count: 0 });

// 這是響應(yīng)式的替換
objectRef.value = { count: 1 };
const obj = {
  foo: ref(1),
  bar: ref(2)
};
// 該函數(shù)接收一個(gè) ref
// 需要通過(guò) .value 取值
// 但它會(huì)保持響應(yīng)性
callSomeFunction(obj.foo);

// 仍然是響應(yīng)式的
const { foo, bar } = obj;

在 html 模板中不需要帶 .value 就可以使用

<script setup>
import { ref } from 'vue';

const count = ref(0);
</script>

<template>
  <div>
    {{ count }}
  </div>
</template>

有狀態(tài)方法

import { reactive, defineComponent, onUnmounted } from 'vue';
import { debounce } from 'lodash-es';

export default defineComponent({
  setup() {
    // 每個(gè)實(shí)例都有了自己的預(yù)置防抖的處理函數(shù)
    const debouncedClick = debounce(click, 500);

    function click() {
      // ... 對(duì)點(diǎn)擊的響應(yīng) ...
    }

    // 最好是在組件卸載時(shí)
    // 清除掉防抖計(jì)時(shí)器
    onUnmounted(() => {
      debouncedClick.cancel();
    });
  },
});

響應(yīng)式樣式

<script setup>
import { ref } from 'vue'
const open = ref(false);
</script>

<template>
  <button @click="open = !open">Toggle</button>
  <div>Hello Vue!</div>  
</template>

<style scope>
  div{
    transition: height 0.1s linear;
    overflow: hidden;
    height: v-bind(open ? '30px' : '0px');
  }
</style>

響應(yīng)式進(jìn)階 —— watch 和 computed

監(jiān)聽(tīng)狀態(tài)

<script setup>
import { ref, watch } from 'vue';

const count = ref(0)
const isEvent = ref(false)

function increment() {
  state.count++
}

watch(count, function() {
  isEvent.value = count.value % 2 === 0
})
</script>

<template>
  <button @click="increment">
    {{ count }}
  </button>
  <p>
    is event: {{ isEvent ? 'yes' : 'no' }}
  </p>
</template>

立即監(jiān)聽(tīng)狀態(tài)

watch(count, function() {
  isEvent.value = count.value % 2 === 0
}, {
  // 上例中的 watch 不會(huì)立即執(zhí)行,導(dǎo)致 isEvent 狀態(tài)的初始值不準(zhǔn)確。配置立即執(zhí)行,會(huì)在一開(kāi)始的時(shí)候立即執(zhí)行一次
  immediate: true
})

計(jì)算狀態(tài)

<script setup>
import { ref, computed } from 'vue';

const text = ref('')
// computed 的回調(diào)函數(shù)里,會(huì)根據(jù)已有并用到的狀態(tài)計(jì)算出新的狀態(tài)
const capital = computed(function(){
  return text.value.toUpperCase();
})
</script>

<template>
  <input v-model="text" />
  <p>to capital: {{ capital }}</p>
</template>

組件通信

defineProps

<script setup>
import { defineProps } from 'vue';

// 這里可以將 `username` 解構(gòu)出來(lái),
// 但是一旦解構(gòu)出來(lái)再使用,就不具備響應(yīng)式能力
defineProps({
  username: String
})
</script>

<template>
  <p>username: {{ username }}</p>
</template>

子組件定義需要的參數(shù)

<script setup>
const username = 'vue'
</script>

<template>
  <children :username="username" />
</template>

父組件參入?yún)?shù)

defineEmits

<script setup>
import { defineEmits, ref } from 'vue';

const emit = defineEmits(['search'])
const keyword = ref('')
const onSearch = function() {
  emit('search', keyword.value)
}
</script>

<template>
  <input v-model="keyword" />
  <button @click="onSearch">search</button>
</template>

子組件定義支持 emit 的函數(shù)

<script setup>
const onSearch = function(keyword){
  console.log(keyword)
}
</script>

<template>
  <children @search="onSearch" />
</template>

父組件綁定子組件定義的事件

defineExpose

<script setup>
import { defineExpose, ref } from 'vue';

const keyword = ref('')
const onSearch = function() {
  console.log(keyword.value)
}

defineExpose({ onSearch })
</script>

<template>
  <input v-model="keyword" />
</template>

子組件對(duì)父組件暴露方法

<script setup>
import { ref } from 'vue'  

const childrenRef = ref(null)
const onSearch = function() {
  childrenRef.value.onSearch()
}
</script>

<template>
  <children ref='childrenRef' />
  <button @click="onSearch">search</button>
</template>

父組件調(diào)用子組件的方法

Provide / Inject

import type { InjectionKey, Ref } from 'vue'

export const ProvideKey = Symbol() as InjectionKey<Ref<string>>

在應(yīng)用中使用 ProvideKey

<script setup lang="ts">
import { provide, ref } from 'vue'
import { ProvideKey } from './types'

const text = ref<string>('123')
provide(ProvideKey, text)
</script>

<template>
  <input v-model="text" />
</template>

父組件為后代組件提供數(shù)據(jù)

<script setup lang="ts">
import { inject } from 'vue'
import { ProvideKey } from './types'

const value = inject(ProvideKey)
</script>

<template>
  <h4>{{value}}</h4>
</template>

后代組件注入父組件提供的數(shù)據(jù)

Vue 中使用 TypeScript

為組件的 props 標(biāo)注類(lèi)型

當(dāng)使用 <script setup> 時(shí),defineProps() 宏函數(shù)支持從它的參數(shù)中推導(dǎo)類(lèi)型

<script setup lang="ts">
const props = defineProps({
  foo: { type: String, required: true },
  bar: Number
})

props.foo // string
props.bar // number | undefined
</script>

對(duì)同一個(gè)文件中的一個(gè)接口或?qū)ο箢?lèi)型字面量的引用:

interface Props {/* ... */}

defineProps<Props>()

Props 解構(gòu)默認(rèn)值

export interface Props {
  msg?: string
  labels?: string[]
}

const props = withDefaults(defineProps<Props>(), {
  msg: 'hello',
  labels: () => ['one', 'two']
})

使用目前為實(shí)驗(yàn)性的響應(yīng)性語(yǔ)法糖

<script setup lang="ts">
interface Props {
  name: string
  count?: number
}

// 對(duì) defineProps() 的響應(yīng)性解構(gòu)
// 默認(rèn)值會(huì)被編譯為等價(jià)的運(yùn)行時(shí)選項(xiàng)
const {
  name, count = 100
} = defineProps<Props>()
</script>

為組件的 emits 標(biāo)注類(lèi)型

<script setup lang="ts">
// 運(yùn)行時(shí)
const emit = defineEmits(['change', 'update'])

// 基于類(lèi)型
const emit = defineEmits<{
  (e: 'change', id: number): void
  (e: 'update', value: string): void
}>()
</script>

為 ref() 標(biāo)注類(lèi)型

ref 會(huì)根據(jù)初始化時(shí)的值推導(dǎo)其類(lèi)型:

import { ref } from 'vue'
import type { Ref } from 'vue'

const year: Ref<string | number> = ref('2020')

year.value = 2020 // 成功!

為 reactive() 標(biāo)注類(lèi)型

import { reactive } from 'vue'

interface Book {
  title: string
  year?: number
}

const book: Book = reactive({
  title: 'Vue 3 指引'
})

為 computed() 標(biāo)注類(lèi)型

你還可以通過(guò)泛型參數(shù)顯式指定類(lèi)型:

const double = computed<number>(() => {
  // 若返回值不是 number 類(lèi)型則會(huì)報(bào)錯(cuò)
})

為事件處理函數(shù)標(biāo)注類(lèi)型

<script setup lang="ts">
function handleChange(event) {
  // `event` 隱式地標(biāo)注為 `any` 類(lèi)型
  console.log(event.target.value)
}
</script>

<template>
  <input
    type="text"
    @change="handleChange" />
</template>

顯式地為事件處理函數(shù)的參數(shù)標(biāo)注類(lèi)型

function handleChange(event: Event) {
  const target = event.target as HTMLInputElement
  console.log(target.value)
}

為 provide / inject 標(biāo)注類(lèi)型

import { provide, inject } from 'vue'
import type { InjectionKey } from 'vue'

const key = Symbol() as InjectionKey<string>
// 若提供的是非字符串值會(huì)導(dǎo)致錯(cuò)誤
provide(key, 'foo')
// foo 的類(lèi)型:string | undefined
const foo = inject(key)

為模板引用標(biāo)注類(lèi)型

<script setup lang="ts">
import { ref, onMounted } from 'vue'

const el = ref<HTMLInputElement | null>(null)

onMounted(() => {
  el.value?.focus()
})
</script>

<template>
  <input ref="el" />
</template>

為組件模板引用標(biāo)注類(lèi)型

<!-- MyModal.vue -->
<script setup lang="ts">
import { ref } from 'vue'

const isContentShown = ref(false)
const open = 
      () => (isContentShown.value = true)

defineExpose({
  open
})
</script>

使用 TypeScript 內(nèi)置的 InstanceType 工具類(lèi)型來(lái)獲取其實(shí)例類(lèi)

<!-- App.vue -->
<script setup lang="ts">
import MyModal from './MyModal.vue'

type Modal = InstanceType<typeof MyModal>

const modal = ref<Modal | null>(null)

const openModal = () => {
  modal.value?.open()
}
</script>

選項(xiàng)式 API 為組件的 props 標(biāo)注類(lèi)型

import { defineComponent } from 'vue'

export default defineComponent({
  // 啟用了類(lèi)型推導(dǎo)
  props: {
    name: String,
    id: [Number, String],
    msg: { type: String, required: true },
    metadata: null
  },
  mounted() {
    // 類(lèi)型:string | undefined
    this.name
    // 類(lèi)型:number|string|undefined
    this.id
    // 類(lèi)型:string
    this.msg
    // 類(lèi)型:any
    this.metadata
  }
})

使用 PropType 這個(gè)工具類(lèi)型來(lái)標(biāo)記更復(fù)雜的 props 類(lèi)型

import { defineComponent } from 'vue'
import type { PropType } from 'vue'

interface Book {
  title: string
  author: string
  year: number
}

export default defineComponent({
  props: {
    book: {
      // 提供相對(duì) `Object` 更確定的類(lèi)型
      type: Object as PropType<Book>,
      required: true
    },
    // 也可以標(biāo)記函數(shù)
    callback: Function as PropType<(id: number) => void>
  },
  mounted() {
    this.book.title // string
    this.book.year // number

    // TS Error: argument of type 'string' is not
    // assignable to parameter of type 'number'
    this.callback?.('123')
  }
})

選項(xiàng)式 API 為組件的 emits 標(biāo)注類(lèi)型

import { defineComponent } from 'vue'

type Payload = { bookName: string }

export default defineComponent({
  emits: {
    addBook(payload: Payload) {
      // 執(zhí)行運(yùn)行時(shí)校驗(yàn)
      return payload.bookName.length > 0
    }
  },
  methods: {
    onSubmit() {
      this.$emit('addBook', {
        bookName: 123 // 類(lèi)型錯(cuò)誤
      })
      // 類(lèi)型錯(cuò)誤
      this.$emit('non-declared-event')
    }
  }
})

選項(xiàng)式 API 為計(jì)算屬性標(biāo)記類(lèi)型

計(jì)算屬性會(huì)自動(dòng)根據(jù)其返回值來(lái)推導(dǎo)其類(lèi)型:

import { defineComponent } from 'vue'

export default defineComponent({
  data() {
    return {
      message: 'Hello!'
    }
  },
  computed: {
    greeting() {
      return this.message + '!'
    }
  },
  mounted() {
    this.greeting // 類(lèi)型:string
  }
})

在某些場(chǎng)景中,你可能想要顯式地標(biāo)記出計(jì)算屬性的類(lèi)型以確保其實(shí)現(xiàn)是正確的:

import { defineComponent } from 'vue'

export default defineComponent({
  data() {
    return {
      message: 'Hello!'
    }
  },
  computed: {
    // 顯式標(biāo)注返回類(lèi)型
    greeting(): string {
      return this.message + '!'
    },

    // 標(biāo)注一個(gè)可寫(xiě)的計(jì)算屬性
    greetingUppercased: {
      get(): string {
        return this.greeting.toUpperCase()
      },
      set(newValue: string) {
        this.message = newValue.toUpperCase()
      }
    }
  }
})

選項(xiàng)式 API 為事件處理函數(shù)標(biāo)注類(lèi)型

import { defineComponent } from 'vue'

export default defineComponent({
  methods: {
    handleChange(event: Event) {
      console.log((event.target as HTMLInputElement).value)
    }
  }
})

選項(xiàng)式 API 擴(kuò)展全局屬性

import axios from 'axios'

declare module 'vue' {
  interface ComponentCustomProperties {
    $http: typeof axios
    $translate: (key: string) => string
  }
}

類(lèi)型擴(kuò)展的位置

我們可以將這些類(lèi)型擴(kuò)展放在一個(gè) .ts 文件,或是一個(gè)影響整個(gè)項(xiàng)目的 *.d.ts 文件中

// 不工作,將覆蓋原始類(lèi)型。
declare module 'vue' {
  interface ComponentCustomProperties {
    $translate: (key: string) => string
  }
}

// 正常工作。
export {}

declare module 'vue' {
  interface ComponentCustomProperties {
    $translate: (key: string) => string
  }
}

選項(xiàng)式 API 擴(kuò)展自定義選項(xiàng)

某些插件,比如 vue-router,提供了一些自定義的組件選項(xiàng),比如 beforeRouteEnter:

import { defineComponent } from 'vue'

export default defineComponent({
  beforeRouteEnter(to, from, next) {
    // ...
  }
})

如果沒(méi)有確切的類(lèi)型標(biāo)注,這個(gè)鉤子函數(shù)的參數(shù)會(huì)隱式地標(biāo)注為 any 類(lèi)型。我們可以為 ComponentCustomOptions 接口擴(kuò)展自定義的選項(xiàng)來(lái)支持:

import { Route } from 'vue-router'

declare module 'vue' {
  interface ComponentCustomOptions {
    beforeRouteEnter?(
      to: Route,
      from: Route,
      next: () => void
    ): void
  }
}

API 參考

全局 API - 應(yīng)用實(shí)例

:-:-
createApp()創(chuàng)建一個(gè)應(yīng)用實(shí)例 #
createSSRApp()SSR 激活模式創(chuàng)建一個(gè)應(yīng)用實(shí)例 #
app.mount()將應(yīng)用實(shí)例掛載在一個(gè)容器元素中 #
app.unmount()卸載一個(gè)已掛載的應(yīng)用實(shí)例 #
app.provide()提供一個(gè)可以在應(yīng)用中的所有后代組件中注入使用的值 #
app.component()注冊(cè)或獲取全局組件 #
app.directive()注冊(cè)或獲取全局指令 #
app.use()安裝一個(gè)插件 #
app.mixin()全局注冊(cè)一個(gè)混入 #
app.version當(dāng)前應(yīng)用所使用的 Vue 版本號(hào) #
app.config獲得應(yīng)用實(shí)例的配置設(shè)定 #
app.config.errorHandler為應(yīng)用內(nèi)拋出的未捕獲錯(cuò)誤指定一個(gè)全局處理函數(shù) #
app.config.warnHandler為 Vue 的運(yùn)行時(shí)警告指定一個(gè)自定義處理函數(shù) #
app.config.performance在瀏覽器開(kāi)發(fā)工具中追蹤性能表現(xiàn) #
app.config.compilerOptions配置運(yùn)行時(shí)編譯器的選項(xiàng) #
app.config.globalProperties注冊(cè)全局屬性對(duì)象 #
app.config.optionMergeStrategies定義自定義組件選項(xiàng)的合并策略的對(duì)象 #

全局 API - 通用

:-:-
versionVue 版本號(hào) #
nextTick()等待下一次 DOM 更新后執(zhí)行回調(diào) #
defineComponent()在定義 Vue 組件時(shí)提供類(lèi)型推導(dǎo)的輔助函數(shù) #
defineAsyncComponent()定義一個(gè)異步組件 #
defineCustomElement()defineComponent 接受的參數(shù)相同,不同的是會(huì)返回一個(gè)原生自定義元素類(lèi)的構(gòu)造器 #

組合式 API - setup()

:-:-
基本使用#
訪問(wèn) Props#
Setup 上下文#
與渲染函數(shù)一起使用#

組合式 API - 依賴注入

:-:-
provide()提供一個(gè)可以被后代組件中注入使用的值 #
inject()注入一個(gè)由祖先組件提供的值 #

組合式 API - 生命周期鉤子

:-:-
onMounted()組件掛載完成后執(zhí)行 #
onUpdated()狀態(tài)變更而更新其 DOM 樹(shù)之后調(diào)用 #
onUnmounted()組件實(shí)例被卸載之后調(diào)用 #
onBeforeMount()組件被掛載之前被調(diào)用 #
onBeforeUpdate()狀態(tài)變更而更新其 DOM 樹(shù)之前調(diào)用 #
onBeforeUnmount()組件實(shí)例被卸載之前調(diào)用 #
onErrorCaptured()捕獲了后代組件傳遞的錯(cuò)誤時(shí)調(diào)用 #
onRenderTracked()組件渲染過(guò)程中追蹤到響應(yīng)式依賴時(shí)調(diào)用 #
onRenderTriggered()響應(yīng)式依賴的變更觸發(fā)了組件渲染時(shí)調(diào)用 #
onActivated()若組件實(shí)例是 <KeepAlive> 緩存樹(shù)的一部分,當(dāng)組件被插入到 DOM 中時(shí)調(diào)用 #
onDeactivated()若組件實(shí)例是 <KeepAlive> 緩存樹(shù)的一部分,當(dāng)組件從 DOM 中被移除時(shí)調(diào)用 #
onServerPrefetch()組件實(shí)例在服務(wù)器上被渲染之前調(diào)用 #

組合式 API - 響應(yīng)式: 工具

:-:-
isRef()判斷是否為 ref #
unref()是 ref,返回內(nèi)部值,否則返回參數(shù)本身 #
toRef()創(chuàng)建一個(gè)屬性對(duì)應(yīng)的 ref #
toRefs()將對(duì)象上的每一個(gè)可枚舉屬性轉(zhuǎn)換為 ref #
isProxy()檢查一個(gè)對(duì)象是否是由 reactive()readonly()、shallowReactive()shallowReadonly() 創(chuàng)建的代理 #
isReactive()檢查一個(gè)對(duì)象是否是由 reactive()shallowReactive() 創(chuàng)建的代理。 #
isReadonly()檢查傳入的值是否為只讀對(duì)象 #

組合式 API - 響應(yīng)式: 核心

:-:-
ref()返回一個(gè) ref 對(duì)象 #
computed ()定義一個(gè)計(jì)算屬性 #
reactive()返回一個(gè)對(duì)象的響應(yīng)式代理 #
readonly()返回一個(gè)原值的只讀代理 #
watchEffect()立即運(yùn)行一個(gè)函數(shù),同時(shí)監(jiān)聽(tīng) #
watchPostEffect()watchEffect() 使用 flush: 'post' 選項(xiàng)時(shí)的別名。 #
watchSyncEffect()watchEffect() 使用 flush: 'sync' 選項(xiàng)時(shí)的別名。 #
watch()偵聽(tīng)一個(gè)或多個(gè)響應(yīng)式數(shù)據(jù)源 #

選項(xiàng)式 API - 狀態(tài)選項(xiàng)

:-:-
data聲明組件初始響應(yīng)式狀態(tài) #
props聲明一個(gè)組件的 props #
computed聲明要在組件實(shí)例上暴露的計(jì)算屬性 #
methods聲明要混入到組件實(shí)例中的方法 #
watch聲明在數(shù)據(jù)更改時(shí)調(diào)用的偵聽(tīng)回調(diào) #
emits聲明由組件觸發(fā)的自定義事件 #
expose聲明當(dāng)組件實(shí)例被父組件通過(guò)模板引用訪問(wèn)時(shí)暴露的公共屬性 #

選項(xiàng)式 API - 生命周期選項(xiàng)

:-:-
beforeCreate組件實(shí)例初始化完成之后立即調(diào)用 #
created組件實(shí)例處理完所有與狀態(tài)相關(guān)的選項(xiàng)后調(diào)用 #
beforeMount組件被掛載之前調(diào)用 #
mounted組件被掛載之后調(diào)用 #
beforeUpdate狀態(tài)變更而更新其 DOM 樹(shù)之前調(diào)用 #
updated狀態(tài)變更而更新其 DOM 樹(shù)之后調(diào)用 #
beforeUnmount組件實(shí)例被卸載之前調(diào)用 #
unmounted組件實(shí)例被卸載之后調(diào)用 #
errorCaptured捕獲了后代組件傳遞的錯(cuò)誤時(shí)調(diào)用 #
renderTracked Dev only組件渲染過(guò)程中追蹤到響應(yīng)式依賴時(shí)調(diào)用 #
renderTriggered Dev only響應(yīng)式依賴的變更觸發(fā)了組件渲染時(shí)調(diào)用 #
activated若組件實(shí)例是 緩存樹(shù)的一部分,當(dāng)組件被插入到 DOM 中時(shí)調(diào)用 #
deactivated若組件實(shí)例是 緩存樹(shù)的一部分,當(dāng)組件從 DOM 中被移除時(shí)調(diào)用 #
serverPrefetch SSR only組件實(shí)例在服務(wù)器上被渲染之前調(diào)用 #

選項(xiàng)式 API - 其他雜項(xiàng)

:-:-
name顯式聲明組件展示時(shí)的名稱(chēng) #
inheritAttrs是否啟用默認(rèn)的組件 attribute 透?jìng)餍袨?#
components注冊(cè)對(duì)當(dāng)前組件實(shí)例可用的組件 #
directives注冊(cè)對(duì)當(dāng)前組件實(shí)例可用的指令 #

選項(xiàng)式 API - 渲染選項(xiàng)

:-:-
template聲明組件的字符串模板 #
render編程式地創(chuàng)建組件虛擬 DOM 樹(shù)的函數(shù) #
compilerOptions配置組件模板的運(yùn)行時(shí)編譯器選項(xiàng) #

選項(xiàng)式 API - 組件實(shí)例

:-:-
$data觀察的數(shù)據(jù)對(duì)象 #
$props組件已解析的 props 對(duì)象 #
$el實(shí)例管理的 DOM 根節(jié)點(diǎn) #
$options實(shí)例的初始化選項(xiàng) #
$parent父實(shí)例 #
$root當(dāng)前組件樹(shù)的根實(shí)例 #
$slots訪問(wèn)被插槽分發(fā)的內(nèi)容 #
$refsDOM 元素和組件實(shí)例 #
$attrs包含了組件所有透?jìng)?attributes #
$watch()觀察 Vue 實(shí)例上的一個(gè)表達(dá)式或者一個(gè)函數(shù)計(jì)算結(jié)果的變化 #
$emit()觸發(fā)一個(gè)自定義事件 #
$forceUpdate()強(qiáng)制該組件重新渲染 #
$nextTick()回調(diào)延遲執(zhí)行 #

選項(xiàng)式 API - 組合選項(xiàng)

:-:-
provide提供可以被后代組件注入的值 #
inject注入一個(gè)由祖先組件提供的值 #
mixins接收一個(gè)混入對(duì)象的數(shù)組 #
extends要繼承的“基類(lèi)”組件 #

內(nèi)置內(nèi)容 - 指令

:-:-
v-text更新元素的 textContent #
v-html更新元素的 innerHTML #
v-show切換元素的 display css 屬性 #
v-if有條件地渲染元素 #
v-else#
v-else-if#
v-for多次渲染元素或模板塊 #
v-on綁定事件監(jiān)聽(tīng)器 #
v-bind動(dòng)態(tài)地綁定一個(gè)或多個(gè)屬性 #
v-model創(chuàng)建雙向綁定 #
v-slot提供插槽或接收 props 的插槽 #
v-pre跳過(guò)元素和它的子元素編譯過(guò)程 #
v-once只渲染元素和組件一次 #
v-memo (3.2+)緩存一個(gè)模板的子樹(shù) #
v-cloak保持在元素上直到實(shí)例結(jié)束編譯 #
serverPrefetch SSR only組件實(shí)例在服務(wù)器上被渲染之前調(diào)用 #

內(nèi)置內(nèi)容 - 組件

:-:-
<Transition>單個(gè)元素/組件的過(guò)渡效果 #
<TransitionGroup>多個(gè)元素/組件的過(guò)渡效果 #
<KeepAlive>緩存包裹在其中的動(dòng)態(tài)切換組件 #
<Teleport>將其插槽內(nèi)容渲染到 DOM 中的另一個(gè)位置 #
<Suspense> (Experimental)協(xié)調(diào)對(duì)組件樹(shù)中嵌套的異步依賴的處理 #

內(nèi)置內(nèi)容 - 特殊 Attributes

:-:-
key用在 Vue 的虛擬 DOM 算法 #
ref元素或子組件注冊(cè)引用信息 #
is綁定動(dòng)態(tài)組件 #

內(nèi)置內(nèi)容 - 特殊元素

:-:-
<component>渲染一個(gè)“元組件”用于動(dòng)態(tài)組件或元素 #
<slot>組件模板中的插槽內(nèi)容出口 #

單文件組件 - 語(yǔ)法定義

:-:-
總覽#
相應(yīng)語(yǔ)言塊#
自動(dòng)名稱(chēng)推導(dǎo)#
預(yù)處理器#
Src 導(dǎo)入#
注釋#

單文件組件 - <script setup>

:-:-
基本語(yǔ)法#
響應(yīng)式#
使用組件#
使用自定義指令#
defineProps() 和 defineEmits()#
defineExpose#
useSlots() 和 useAttrs()#
與普通的 &lt;script&gt; 一起使用#
頂層 await#
針對(duì) TypeScript 的功能#
限制#

單文件組件 - CSS 功能

:-:-
組件作用域 CSS#
CSS Modules#
CSS 中的 v-bind()#

進(jìn)階 API - 渲染函數(shù)

:-:-
h()創(chuàng)建虛擬 DOM 節(jié)點(diǎn) #
mergeProps()合并多個(gè) props 對(duì)象 #
cloneVNode()克隆一個(gè) vnode #
isVNode()判斷一個(gè)值是否為 vnode 類(lèi)型 #
resolveComponent()按名稱(chēng)手動(dòng)解析已注冊(cè)的組件 #
resolveDirective()按名稱(chēng)手動(dòng)解析已注冊(cè)的指令 #
withDirectives()用于給 vnode 增加自定義指令 #
withModifiers()用于向事件處理函數(shù)添加內(nèi)置 v-on 修飾符 #

進(jìn)階 API - 服務(wù)端渲染

:-:-
renderToString()#
renderToNodeStream()#
pipeToNodeWritable()#
renderToWebStream()#
pipeToWebWritable()#
renderToSimpleStream()#
useSSRContext()#

進(jìn)階 API - TypeScript 工具類(lèi)型

:-:-
PropType<T>在用運(yùn)行時(shí) props 聲明時(shí)給一個(gè) prop 標(biāo)注更復(fù)雜的類(lèi)型定義 #
ComponentCustomProperties增強(qiáng)組件實(shí)例類(lèi)型以支持自定義全局屬性 #
ComponentCustomOptions擴(kuò)展組件選項(xiàng)類(lèi)型以支持自定義選項(xiàng) #
ComponentCustomProps擴(kuò)展全局可用的 TSX props #
CSSProperties擴(kuò)展在樣式屬性綁定上允許的值的類(lèi)型 #

進(jìn)階 API - 自定義渲染

:-:-
createRenderer()創(chuàng)建一個(gè)自定義渲染器 #