Vue 3.0 全局API

2021-07-16 11:26 更新

#createApp

返回一個提供應用上下文的應用實例。應用實例掛載的整個組件樹共享同一個上下文。

  1. const app = Vue.createApp({})

你可以在 createApp 之后鏈式調(diào)用其它方法,這些方法可以在應用 API 中找到。

#參數(shù)

該函數(shù)接收一個根組件選項對象作為第一個參數(shù):

  1. const app = Vue.createApp({
  2. data() {
  3. return {
  4. ...
  5. }
  6. },
  7. methods: {...},
  8. computed: {...}
  9. ...
  10. })

使用第二個參數(shù),我們可以將根 prop 傳遞給應用程序:

  1. const app = Vue.createApp(
  2. {
  3. props: ['username']
  4. },
  5. { username: 'Evan' }
  6. )

  1. <div id="app">
  2. <!-- 會顯示 'Evan' -->
  3. {{ username }}
  4. </div>

#類型聲明

  1. interface Data {
  2. [key: string]: unknown
  3. }
  4. export type CreateAppFunction<HostElement> = (
  5. rootComponent: PublicAPIComponent,
  6. rootProps?: Data | null
  7. ) => App<HostElement>

#h

返回一個”虛擬節(jié)點“,通??s寫為 VNode:一個普通對象,其中包含向 Vue 描述它應在頁面上渲染哪種節(jié)點的信息,包括所有子節(jié)點的描述。它的目的是用于手動編寫的渲染函數(shù)

  1. render() {
  2. return Vue.h('h1', {}, 'Some title')
  3. }

#參數(shù)

接收三個參數(shù):type,propschildren

#type

  • 類型:String | Object | Function

  • 詳細:

HTML 標簽名、組件或異步組件。使用返回 null 的函數(shù)將渲染一個注釋。此參數(shù)是必需的。

#props

  • 類型:Object

  • 詳細:

一個對象,與我們將在模板中使用的 attribute、prop 和事件相對應??蛇x。

#children

  • 類型:String | Array | Object

  • 詳細:

子代 VNode,使用 h() 生成,或者使用字符串來獲取“文本 VNode”,或帶有插槽的對象??蛇x。

  1. h('div', {}, [
  2. 'Some text comes first.',
  3. h('h1', 'A headline'),
  4. h(MyComponent, {
  5. someProp: 'foobar'
  6. })
  7. ])

#defineComponent

從實現(xiàn)上看,defineComponent 只返回傳遞給它的對象。但是,就類型而言,返回的值有一個合成類型的構(gòu)造函數(shù),用于手動渲染函數(shù)、TSX 和 IDE 工具支持。

#參數(shù)

具有組件選項的對象

  1. import { defineComponent } from 'vue'
  2. const MyComponent = defineComponent({
  3. data() {
  4. return { count: 1 }
  5. },
  6. methods: {
  7. increment() {
  8. this.count++
  9. }
  10. }
  11. })

或者是一個 setup 函數(shù),函數(shù)名稱將作為組件名稱來使用

  1. import { defineComponent, ref } from 'vue'
  2. const HelloWorld = defineComponent(function HelloWorld() {
  3. const count = ref(0)
  4. return { count }
  5. })

#defineAsyncComponent

創(chuàng)建一個只有在需要時才會加載的異步組件。

#參數(shù)

對于基本用法,defineAsyncComponent 可以接受一個返回 Promise 的工廠函數(shù)。Promise 的 resolve 回調(diào)應該在服務端返回組件定義后被調(diào)用。你也可以調(diào)用 reject(reason) 來表示加載失敗。

  1. import { defineAsyncComponent } from 'vue'
  2. const AsyncComp = defineAsyncComponent(() =>
  3. import('./components/AsyncComponent.vue')
  4. )
  5. app.component('async-component', AsyncComp)

當使用局部注冊時,你也可以直接提供一個返回 Promise 的函數(shù):

  1. import { createApp, defineAsyncComponent } from 'vue'
  2. createApp({
  3. // ...
  4. components: {
  5. AsyncComponent: defineAsyncComponent(() =>
  6. import('./components/AsyncComponent.vue')
  7. )
  8. }
  9. })

對于高階用法,defineAsyncComponent 可以接受一個對象:

defineAsyncComponent 方法還可以返回以下格式的對象:

  1. import { defineAsyncComponent } from 'vue'
  2. const AsyncComp = defineAsyncComponent({
  3. // 工廠函數(shù)
  4. loader: () => import('./Foo.vue')
  5. // 加載異步組件時要使用的組件
  6. loadingComponent: LoadingComponent,
  7. // 加載失敗時要使用的組件
  8. errorComponent: ErrorComponent,
  9. // 在顯示 loadingComponent 之前的延遲 | 默認值:200(單位 ms)
  10. delay: 200,
  11. // 如果提供了 timeout ,并且加載組件的時間超過了設定值,將顯示錯誤組件
  12. // 默認值:Infinity(即永不超時,單位 ms)
  13. timeout: 3000,
  14. // 定義組件是否可掛起 | 默認值:true
  15. suspensible: false,
  16. /**
  17. *
  18. * @param {*} error 錯誤信息對象
  19. * @param {*} retry 一個函數(shù),用于指示當 promise 加載器 reject 時,加載器是否應該重試
  20. * @param {*} fail 一個函數(shù),指示加載程序結(jié)束退出
  21. * @param {*} attempts 允許的最大重試次數(shù)
  22. */
  23. onError(error, retry, fail, attempts) {
  24. if (error.message.match(/fetch/) && attempts <= 3) {
  25. // 請求發(fā)生錯誤時重試,最多可嘗試 3 次
  26. retry()
  27. } else {
  28. // 注意,retry/fail 就像 promise 的 resolve/reject 一樣:
  29. // 必須調(diào)用其中一個才能繼續(xù)錯誤處理。
  30. fail()
  31. }
  32. }
  33. })

參考動態(tài)和異步組件

#resolveComponent

WARNING

resolveComponent 只能在 rendersetup 函數(shù)中使用。

如果在當前應用實例中可用,則允許按名稱解析 component。

返回一個 Component。如果沒有找到,則返回 undefined

  1. const app = Vue.createApp({})
  2. app.component('MyComponent', {
  3. /* ... */
  4. })

  1. import { resolveComponent } from 'vue'
  2. render() {
  3. const MyComponent = resolveComponent('MyComponent')
  4. }

#參數(shù)

接受一個參數(shù):name

#name

  • 類型:String

  • 詳細:

已加載的組件的名稱。

#resolveDynamicComponent

WARNING

resolveDynamicComponent 只能在 rendersetup 函數(shù)中使用。

允許使用與 <component :is=""> 相同的機制來解析一個 component。

返回已解析的 Component 或新創(chuàng)建的 VNode,其中組件名稱作為節(jié)點標簽。如果找不到 Component,將發(fā)出警告。

  1. import { resolveDynamicComponent } from 'vue'
  2. render () {
  3. const MyComponent = resolveDynamicComponent('MyComponent')
  4. }

#參數(shù)

接受一個參數(shù):component

#component

  • 類型:String | Object (組件的選項對象)

  • 詳細:

有關(guān)詳細信息,請參閱動態(tài)組件上的文檔。

#resolveDirective

WARNING

resolveDirective 只能在 rendersetup 函數(shù)中使用。

如果在當前應用實例中可用,則允許通過其名稱解析一個 directive

返回一個 Directive。如果沒有找到,則返回 undefined。

  1. const app = Vue.createApp({})
  2. app.directive('highlight', {})

  1. import { resolveDirective } from 'vue'
  2. render () {
  3. const highlightDirective = resolveDirective('highlight')
  4. }

#參數(shù)

接受一個參數(shù):name

#name

  • 類型:String

  • 詳細:

已加載的指令的名稱。

#withDirectives

WARNING

withDirectives 只能在 rendersetup 函數(shù)中使用。

允許將指令應用于 VNode。返回一個包含應用指令的 VNode。

  1. import { withDirectives, resolveDirective } from 'vue'
  2. const foo = resolveDirective('foo')
  3. const bar = resolveDirective('bar')
  4. return withDirectives(h('div'), [
  5. [foo, this.x],
  6. [bar, this.y]
  7. ])

#參數(shù)

接受兩個參數(shù):vnodedirectives

#vnode

  • 類型:vnode

  • 詳細:

一個虛擬節(jié)點,通常使用 h() 創(chuàng)建。

#directives

  • 類型:Array

  • 詳細:

一個指令數(shù)組。

每個指令本身都是一個數(shù)組,最多可以定義 4 個索引,如以下示例所示。

  • [directive] - 該指令本身。必選。

  1. const MyDirective = resolveDirective('MyDirective')
  2. const nodeWithDirectives = withDirectives(h('div'), [[MyDirective]])

  • [directive, value] - 上述內(nèi)容,再加上分配給指令的類型為 any 的值。

  1. const MyDirective = resolveDirective('MyDirective')
  2. const nodeWithDirectives = withDirectives(h('div'), [[MyDirective, 100]])

  • [directive, value, arg] - 上述內(nèi)容,再加上一個 string 參數(shù),比如:在 v-on:click 中的 click

  1. const MyDirective = resolveDirective('MyDirective')
  2. const nodeWithDirectives = withDirectives(h('div'), [
  3. [MyDirective, 100, 'click']
  4. ])

  • [directive, value, arg, modifiers] - 上述內(nèi)容,再加上定義任何修飾符的 key: value 鍵值對 Object。

  1. const MyDirective = resolveDirective('MyDirective')
  2. const nodeWithDirectives = withDirectives(h('div'), [
  3. [MyDirective, 100, 'click', { prevent: true }]
  4. ])

#createRenderer

createRenderer 函數(shù)接受兩個泛型參數(shù): HostNodeHostElement,對應于宿主環(huán)境中的 Node 和 Element 類型。

例如,對于 runtime-dom,HostNode 將是 DOM Node 接口,HostElement 將是 DOM Element 接口。

自定義渲染器可以傳入特定于平臺的類型,如下所示:

  1. import { createRenderer } from 'vue'
  2. const { render, createApp } = createRenderer<Node, Element>({
  3. patchProp,
  4. ...nodeOps
  5. })

#參數(shù)

接受兩個參數(shù):HostNodeHostElement。

#HostNode

  • 類型:Node

  • 詳細:

宿主環(huán)境中的節(jié)點。

#HostElement

  • 類型:Element

  • 詳細:

宿主環(huán)境中的元素。

#nextTick

將回調(diào)推遲到下一個 DOM 更新周期之后執(zhí)行。在更改了一些數(shù)據(jù)以等待 DOM 更新后立即使用它。

  1. import { createApp, nextTick } from 'vue'
  2. const app = createApp({
  3. setup() {
  4. const message = ref('Hello!')
  5. const changeMessage = async newMessage => {
  6. message.value = newMessage
  7. await nextTick()
  8. console.log('Now DOM is updated')
  9. }
  10. }
  11. })

參考$nextTick 實例方法

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號