漸進(jìn)式 JavaScript 框架 Vue 3 備忘清單的快速參考列表,包含常用 API 和示例
Vue 是一套用于構(gòu)建用戶界面的漸進(jìn)式框架
注意:Vue 3.x 版本對(duì)應(yīng) Vue Router 4.x 路由版本
已安裝 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)建版本
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>
<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>
<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>
<span>Message: {{ msg }}</span>
使用的是 Mustache 語(yǔ)法 (即雙大括號(hào)),每次 msg 屬性更改時(shí)它也會(huì)同步更新
<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
<div v-bind:id="dynamicId"></div>
簡(jiǎn)寫(xiě)
<div :id="dynamicId"></div>
<button :disabled="isButtonDisabled">
Button
</button>
通過(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
})
{{ number + 1 }}
{{ ok ? 'YES' : 'NO' }}
{{ message.split('').reverse().join('') }}
<div :id="`list-${id}`"></div>
<!-- 這是一個(gè)語(yǔ)句,而非表達(dá)式 -->
{{ var a = 1 }}
<!-- 條件控制也不支持,請(qǐng)使用三元表達(dá)式 -->
{{ if (ok) { return message } }}
<span :title="toTitleDate(date)">
{{ formatDate(date) }}
</span>
<p v-if="seen">Now you see me</p>
<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>
<a v-bind:[attributeName]="url"> ... </a>
<!-- 簡(jiǎn)寫(xiě) -->
<a :[attributeName]="url"> ... </a>
這里的 attributeName 會(huì)作為一個(gè) JS 表達(dá)式被動(dòng)態(tài)執(zhí)行
<a v-on:[eventName]="doSomething"> ... </a>
<!-- 簡(jiǎn)寫(xiě) -->
<a @[eventName]="doSomething">
<form @submit.prevent="onSubmit">
...
</form>
.prevent 修飾符會(huì)告知 v-on 指令對(duì)觸發(fā)的事件調(diào)用 event.preventDefault()
v-on:submit.prevent="onSubmit"
──┬─ ─┬──── ─┬───── ─┬──────
┆ ┆ ┆ ╰─ Value 解釋為JS表達(dá)式
┆ ┆ ╰─ Modifiers 由前導(dǎo)點(diǎn)表示
┆ ╰─ Argument 跟隨冒號(hào)或速記符號(hào)
╰─ Name 以 v- 開(kāi)頭使用速記時(shí)可以省略
<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>
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();
});
},
});
<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>
<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>
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
})
<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>
<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ù)
<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>
父組件綁定子組件定義的事件
<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)用子組件的方法
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ù)
當(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>()
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>
<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 會(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 // 成功!
import { reactive } from 'vue'
interface Book {
title: string
year?: number
}
const book: Book = reactive({
title: 'Vue 3 指引'
})
你還可以通過(guò)泛型參數(shù)顯式指定類(lèi)型:
const double = computed<number>(() => {
// 若返回值不是 number 類(lèi)型則會(huì)報(bào)錯(cuò)
})
<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)
}
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)
<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>
<!-- 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>
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')
}
})
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')
}
}
})
計(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()
}
}
}
})
import { defineComponent } from 'vue'
export default defineComponent({
methods: {
handleChange(event: Event) {
console.log((event.target as HTMLInputElement).value)
}
}
})
import axios from 'axios'
declare module 'vue' {
interface ComponentCustomProperties {
$http: typeof axios
$translate: (key: string) => string
}
}
我們可以將這些類(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
}
}
某些插件,比如 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
}
}
| :- | :- |
|---|---|
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ì)象 # |
| :- | :- |
|---|---|
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)用 # |
| :- | :- |
|---|---|
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ì)象 # |
| :- | :- |
|---|---|
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ù)源 # |
| :- | :- |
|---|---|
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í)例是 |
deactivated | 若組件實(shí)例是 |
serverPrefetch SSR only | 組件實(shí)例在服務(wù)器上被渲染之前調(diào)用 # |
| :- | :- |
|---|---|
$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)容 # |
$refs | DOM 元素和組件實(shí)例 # |
$attrs | 包含了組件所有透?jìng)?attributes # |
$watch() | 觀察 Vue 實(shí)例上的一個(gè)表達(dá)式或者一個(gè)函數(shù)計(jì)算結(jié)果的變化 # |
$emit() | 觸發(fā)一個(gè)自定義事件 # |
$forceUpdate() | 強(qiáng)制該組件重新渲染 # |
$nextTick() | 回調(diào)延遲執(zhí)行 # |
| :- | :- |
|---|---|
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)用 # |
| :- | :- |
|---|---|
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 修飾符 # |
| :- | :- |
|---|---|
createRenderer() | 創(chuàng)建一個(gè)自定義渲染器 # |
感谢您访问我们的网站,您可能还对以下资源感兴趣:
国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片