Skip to content

Global config and Global API reference #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const sidebar = {
collapsable: true,
children: ['writing-guide']
}
]
],
api: ['/api/application-config', '/api/application-api']
}

module.exports = {
Expand All @@ -67,6 +68,7 @@ module.exports = {
rel: 'stylesheet',
},
],
['link', { rel: 'icon', href: '/logo.png' }],
[
'script',
{
Expand All @@ -92,7 +94,7 @@ module.exports = {
{ text: 'Tooling', link: '/tooling/' }
]
},
{ text: 'API Reference', link: '/api/' },
{ text: 'API Reference', link: '/api/application-config' },
{
text: 'Examples',
ariaLabel: 'Examples Menu',
Expand All @@ -114,8 +116,10 @@ module.exports = {
],
sidebarDepth: 2,
sidebar: {
collapsable: false,
'/guide/': sidebar.guide,
'/community/': sidebar.guide
'/community/': sidebar.guide,
'/api/': sidebar.api
},
smoothScroll: false
},
Expand Down
274 changes: 274 additions & 0 deletions src/api/application-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
# Application API

In Vue 3, APIs that globally mutate Vue's behavior are now moved to application instances created by the new `createApp` method. In addition, their effects are now scoped to that specific application's instance:

```js
import { createApp } from 'vue'

const app = createApp({})
```

Calling `createApp` returns an application instance. This instance provides an application context. The entire component tree mounted by the application instance share the same context, which provides the configurations that were previously "global" in Vue 2.x.

In addition, since the `createApp` method returns the application instance itself, you can chain other methods after it which can be found in the following sections.

## component

- **Arguments:**

- `{string} name`
- `{Function | Object} [definition]`

- **Usage:**

Register or retrieve a global component. Registration also automatically sets the component's `name` with the given `name` parameter.

- **Example:**

```js
import { createApp } from 'vue'

const app = createApp({})

// register an options object
app.component('my-component', {
/* ... */
})

// retrieve a registered component (always return constructor)
const MyComponent = app.component('my-component', {})
```

- **See also:** [Components](../guide/component-basics.html)

## config

- **Usage:**

An object containing application configurations.

- **Example:**

```js
import { createApp } from 'vue'
const app = createApp({})

app.config = {...}
```

- **See also:** [Global Config Properties](./global-config.html)

## directive

- **Arguments:**

- `{string} name`
- `{Function | Object} [definition]`

- **Usage:**

Register or retrieve a global directive.

- **Example:**

```js
import { createApp } from 'vue'
const app = createApp({})

// register
app.directive('my-directive', {
// Directive has a set of lifecycle hooks:
// called before bound element's parent component is mounted
beforeMount() {},
// called when bound element's parent component is mounted
mounted() {},
// called before the containing component's VNode is updated
beforeUpdate() {},
// called after the containing component's VNode and the VNodes of its children // have updated
updated() {},
// called before the bound element's parent component is unmounted
beforeUnmount() {},
// called when the bound element's parent component is unmounted
unmounted() {}
})

// register (function directive)
app.directive('my-directive', () => {
// this will be called as `mounted` and `updated`
})

// getter, return the directive definition if registered
const myDirective = app.directive('my-directive')
```

Directive hooks are passed these arguments:

#### el

The element the directive is bound to. This can be used to directly manipulate the DOM.

#### binding

An object containing the following properties.

- `instance`: The instance of the component where directive is used.
- `value`: The value passed to the directive. For example in `v-my-directive="1 + 1"`, the value would be `2`.
- `oldValue`: The previous value, only available in `beforeUpdate` and `updated`. It is available whether or not the value has changed.
- `arg`: The argument passed to the directive, if any. For example in `v-my-directive:foo`, the arg would be `"foo"`.
- `modifiers`: An object containing modifiers, if any. For example in `v-my-directive.foo.bar`, the modifiers object would be `{ foo: true, bar: true }`.
- `dir`: an object, passed as a parameter when directive is registered. For example, in the directive

```js
app.directive('focus', {
mounted(el) {
el.focus()
}
})
```

`dir` would be the following object:

```js
{
mounted(el) {
el.focus()
}
}
```

#### vnode

A blueprint of the real DOM element received as el argument above. See the [VNode API](TODO) for full details.

#### prevNode

The previous virtual node, only available in the `beforeUpdate` and `updated` hooks.

:::tip Note
Apart from `el`, you should treat these arguments as read-only and never modify them. If you need to share information across hooks, it is recommended to do so through element's [dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset).
:::

- **See also:** [Custom Directives](../guide/custom-directive.html)

## mixin

- **Arguments:**

- `{Object} mixin`

- **Usage:**

Apply a mixin in the whole application scope, which will affect **every** Vue instance created afterwards in the given app (for example, child components). This can be used by plugin authors to inject custom behavior into components. **Not recommended in application code**.

- **See also:** [Global Mixin](../guide/mixins.html#global-mixin)

## mount

- **Arguments:**

- `{Element | string} rootContainer`
- `{boolean} isHydrate`

- **Usage:**

Mounts a root component of the application instance on the provided DOM element.

- **Example:**

```html
<body>
<div id="my-app"></div>
</body>
```

```js
import { createApp } from 'vue'

const app = createApp({})
// do some necessary preparations
app.mount('#my-app')
```

- **See also:**
- [Lifecycle Diagram](../guide/instance.html#lifecycle-diagram)

## provide

- **Type:**

- `Object | () => Object`

- **Details:**

This option is used together with [`inject`](TODO:inject) are used together to allow an ancestor component to serve as a dependency injector for all its descendants, regardless of how deep the component hierarchy is, as long as they are in the same parent chain.

The `provide` option should be an object or a function that returns an object. This object contains the properties that are available for injection into its descendants. You can use ES2015 Symbols as keys in this object, but only in environments that natively support `Symbol` and `Reflect.ownKeys`.

> Note: the `provide` and `inject` bindings are NOT reactive. This is intentional. However, if you pass down an observed object, properties on that object do remain reactive.

- **Example:**

```js
import { createApp } from 'vue'

const app = createApp({
provide: {
user: 'John Doe'
}
})

app.component('user-card', {
inject: ['user'],
template: `
<div>
{{ user }}
</div>
`
})
```

- **See also:**
- [Provide / Inject](../guide/component-provide-inject.md)

## unmount

- **Arguments:**

- `{Element | string} rootContainer`

- **Usage:**

Unmounts a root component of the application instance on the provided DOM element.

- **Example:**

```html
<body>
<div id="my-app"></div>
</body>
```

```js
import { createApp } from 'vue'

const app = createApp({})
// do some necessary preparations
app.mount('#my-app')

// Application will be unmounted 5 seconds after mount
setTimeout(() => app.unmount('#my-app'), 5000)
```

## use

- **Arguments:**

- `{Object | Function} plugin`

- **Usage:**

Install a Vue.js plugin. If the plugin is an Object, it must expose an `install` method. If it is a function itself, it will be treated as the install method. The install method will be called with Vue as the argument.

When this method is called on the same plugin multiple times, the plugin will be installed only once.

- **See also:** [Plugins](TODO)
Loading