Skip to content

Commit

Permalink
add resolveComponent, resolveDirective, createRenderer and withDirect…
Browse files Browse the repository at this point in the history
…ives to global API (vuejs#255)

* add resolveComponent, resolveDirective and withDirectives

* fix setup to render

* add createRenderer

* fix indentation, add examples, fix args
  • Loading branch information
marina-mosti committed Jul 20, 2020
1 parent 1ba104e commit d3ad441
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions src/api/global-api.md
Expand Up @@ -193,6 +193,195 @@ const AsyncComp = defineAsyncComponent({

**See also**: [Dynamic and Async components](../guide/component-dynamic-async.html)

## resolveComponent

:::warning
`resolveComponent` can only be used within render functions.
:::

Allows resolving a `component` by its name, if it is available in the current application instance.

Returns a `Component` or `undefined` when not found.

```js
const app = Vue.createApp({})
app.component('MyComponent', {
/* ... */
})
```

```js
import { resolveComponent } from 'vue'
render() {
const MyComponent = resolveComponent('MyComponent')
}
```

### Arguments

Accepts one argument: `component`

#### component

- **Type:** `String`

- **Details:**

The name of a loaded component.

## resolveDirective

:::warning
`resolveDirective` can only be used within render functions.
:::

Allows resolving a `directive` by its name, if it is available in the current application instance.

Returns a `Directive` or `undefined` when not found.

```js
const app = Vue.createApp({})
app.directive('highlight', {})
```

```js
import { resolveDirective } from 'vue'
render () {
const highlightDirective = resolveDirective('highlight')
}
```

### Arguments

Accepts one argument: `name`

#### name

- **Type:** `String`

- **Details:**

The name of a loaded directive.

## withDirectives

:::warning
`withDirectives` can only be used within render functions.
:::

Allows applying directives to a **VNode**. Returns a VNode with the applied directives.

```js
import { withDirectives, resolveDirective } from 'vue'
const foo = resolveDirective('foo')
const bar = resolveDirective('bar')

return withDirectives(h('div'), [
[foo, this.x],
[bar, this.y]
])
```

### Arguments

Accepts two arguments: `vnode` and `directives`.

#### vnode

- **Type:** `vnode`

- **Details:**

A virtual node, usually created with `h()`.

#### directives

- **Type:** `Array`

- **Details:**

An array of directives.

Each directive itself is an array, which allows for up to 4 indexes to be defined as seen in the following examples.

- `[directive]` - The directive by itself. Required.

```js
const MyDirective = resolveDirective('MyDirective')
const nodeWithDirectives = withDirectives(
h('div'),
[ [MyDirective] ]
)
```

- `[directive, value]` - The above, plus a value of type `any` to be assigned to the directive

```js
const MyDirective = resolveDirective('MyDirective')
const nodeWithDirectives = withDirectives(
h('div'),
[ [MyDirective, 100] ]
)
```

- `[directive, value, arg]` - The above, plus a `String` argument, ie. `click` in `v-on:click`

```js
const MyDirective = resolveDirective('MyDirective')
const nodeWithDirectives = withDirectives(
h('div'),
[ [MyDirective, 100, 'click'] ]
)
```

- `[directive, value, arg, modifiers]` - The above, plus a `key: value` pair `Object` defining any modifiers.

```js
const MyDirective = resolveDirective('MyDirective')
const nodeWithDirectives = withDirectives(
h('div'),
[ [MyDirective, 100, 'click', { prevent: true }] ]
)
```

## createRenderer

The createRenderer function accepts two generic arguments:
`HostNode` and `HostElement`, corresponding to Node and Element types in the
host environment.

For example, for runtime-dom, HostNode would be the DOM
`Node` interface and HostElement would be the DOM `Element` interface.

Custom renderers can pass in the platform specific types like this:
``` js
import { createRenderer } from 'vue'
const { render, createApp } = createRenderer<Node, Element>({
patchProp,
...nodeOps
})
```

### Arguments

Accepts two arguments: `HostNode` and `HostElement`

#### HostNode

- **Type:** `Node`

- **Details:**

The node in the host environment.

#### HostElement

- **Type:** `Element`

- **Details:**

The element in the host environment.

## nextTick

Defer the callback to be executed after the next DOM update cycle. Use it immediately after you’ve changed some data to wait for the DOM update.
Expand Down

0 comments on commit d3ad441

Please sign in to comment.