Skip to content

Commit 212efc4

Browse files
NataliaTepluhinaznckpikax
authored
Global APIs (#111)
* feat: added composition API reference * fix: changed define component link * Update src/api/composition-api.md Co-authored-by: Rahul Kadyan <hi@znck.me> * Update src/api/composition-api.md Co-authored-by: Rahul Kadyan <hi@znck.me> * feat: added composition API reference * fix: changed define component link * fix: move arguments above usecases * feat: added example for attrs destructuring * fix: fixed `this` usage explanation * feat: added explanation about inline event handler * fix: added example to `isReactive` * fix: grammar * fix: grammar * fix: removed raw values mention * Addressed reviewers comments * feat: moved reactivity APIs * feat: moved reactivity utilities * feat: separated advanced reactivity apis * fix: fixed indentation * fix: refactored reactivity API structure * feat: added links to composition API * fix: renamed proxy APIs to basic * fix: fixed reactive typing * fix: addressed reviewers comments * fix: added async onInvalidate * feat: added example for generic with unknown type * feat: created table of contents * feat: added createApp description * feat: added h * feat: added defineComponent * chore: fixed todos * Update src/api/global-api.md Co-authored-by: Carlos Rodrigues <david-181@hotmail.com> * Update src/api/global-api.md Co-authored-by: Carlos Rodrigues <david-181@hotmail.com> * Update src/api/global-api.md Co-authored-by: Carlos Rodrigues <david-181@hotmail.com> * fix: reordered examples Co-authored-by: Rahul Kadyan <hi@znck.me> Co-authored-by: Carlos Rodrigues <david-181@hotmail.com>
1 parent 1ae2abd commit 212efc4

16 files changed

+244
-25
lines changed

src/.vuepress/config.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const sidebar = {
1414
'/guide/list',
1515
'/guide/events',
1616
'/guide/forms',
17-
'/guide/component-basics',
17+
'/guide/component-basics'
1818
]
1919
},
2020
{
@@ -53,7 +53,12 @@ const sidebar = {
5353
{
5454
title: 'Scaling Up',
5555
collapsable: false,
56-
children: ['/guide/routing', '/guide/state-management', '/guide/ssr', '/guide/accessibility']
56+
children: [
57+
'/guide/routing',
58+
'/guide/state-management',
59+
'/guide/ssr',
60+
'/guide/accessibility'
61+
]
5762
},
5863
{
5964
title: 'Migration to Vue 3',
@@ -69,6 +74,7 @@ const sidebar = {
6974
api: [
7075
'/api/application-config',
7176
'/api/application-api',
77+
'/api/global-api',
7278
{
7379
title: 'Options',
7480
collapsable: false,

src/api/application-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,4 @@ setTimeout(() => app.unmount('#my-app'), 5000)
271271

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

274-
- **See also:** [Plugins](TODO)
274+
- **See also:** [Plugins](../guide/plugins.html)

src/api/composition-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function setup(props: Data, context: SetupContext): Data
170170
```
171171

172172
::: tip
173-
To get type inference for the arguments passed to `setup()`, the use of [`defineComponent`](TODO) is needed.
173+
To get type inference for the arguments passed to `setup()`, the use of [defineComponent](global-api.html#definecomponent) is needed.
174174
:::
175175

176176
## Lifecycle Hooks

src/api/computed-watch-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ type StopHandle = () => void
218218

219219
## `watch`
220220

221-
The `watch` API is the exact equivalent of the Options API [this.\$watch](./instance-methods.html#watch) (and the corresponding [watch](./options-data.html#watch) option). `watch` requires watching a specific data source and applies side effects in a separate callback function. It also is lazy by default - i.e. the callback is only called when the watched source has changed.
221+
The `watch` API is the exact equivalent of the Options API [this.$watch](./instance-methods.html#watch) (and the corresponding [watch](./options-data.html#watch) option). `watch` requires watching a specific data source and applies side effects in a separate callback function. It also is lazy by default - i.e. the callback is only called when the watched source has changed.
222222

223223
- Compared to [watchEffect](#watcheffect), `watch` allows us to:
224224

src/api/global-api.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# Global API
2+
3+
## createApp
4+
5+
Returns an application instance which provides an application context. The entire component tree mounted by the application instance share the same context.
6+
7+
```js
8+
const app = Vue.createApp({})
9+
```
10+
11+
You can chain other methods after `createApp`, they can be found in [Application API](./application-api.html)
12+
13+
### Arguments
14+
15+
The function receives a root component options object as a first parameter:
16+
17+
```js
18+
const app = Vue.createApp({
19+
data() {
20+
return {
21+
...
22+
}
23+
},
24+
methods: {...},
25+
computed: {...}
26+
...
27+
})
28+
```
29+
30+
With the second parameter, we can pass root props to the application:
31+
32+
```js
33+
const app = Vue.createApp(
34+
{
35+
props: ['username']
36+
},
37+
{ username: 'Evan' }
38+
)
39+
```
40+
41+
42+
```html
43+
<div id="app">
44+
<!-- Will display 'Evan' -->
45+
{{ username }}
46+
</div>
47+
```
48+
49+
### Typing
50+
51+
```ts
52+
interface Data {
53+
[key: string]: unknown
54+
}
55+
56+
export type CreateAppFunction<HostElement> = (
57+
rootComponent: PublicAPIComponent,
58+
rootProps?: Data | null
59+
) => App<HostElement>
60+
```
61+
62+
## h
63+
64+
Returns a returns "virtual node", usually abbreviated to **VNode**: a plain object which contains information describing to Vue what kind of node it should render on the page, including descriptions of any child nodes. It is intended for manually written [render functions](../guide/render-function.md):
65+
66+
```js
67+
render() {
68+
return Vue.h('h1', {}, 'Some title')
69+
}
70+
```
71+
72+
### Arguments
73+
74+
Accepts three arguments: `tag`, `props` and `children`
75+
76+
#### tag
77+
78+
- **Type:** `String | Object | Function | null`
79+
80+
- **Details:**
81+
82+
An HTML tag name, a component, an async component or null. Using null would render a comment. This parameter is required
83+
84+
#### props
85+
86+
- **Type:** `Object`
87+
88+
- **Details:**
89+
90+
An object corresponding to the attributes, props and events we would use in a template. Optional
91+
92+
#### children
93+
94+
- **Type:** `String | Array | Object`
95+
96+
- **Details:**
97+
98+
Children VNodes, built using `h()`, or using strings to get "text VNodes" or an object with slots. Optional
99+
100+
```js
101+
h('div', {}, [
102+
'Some text comes first.',
103+
h('h1', 'A headline'),
104+
h(MyComponent, {
105+
someProp: 'foobar'
106+
})
107+
])
108+
```
109+
110+
## defineComponent
111+
112+
Implementation-wise `defineComponent` does nothing but return the object passed to it. However, in terms of typing, the returned value has a synthetic type of a constructor for manual render function, TSX and IDE tooling support.
113+
114+
### Arguments
115+
116+
An object with component options
117+
118+
```js
119+
import { defineComponent } from 'vue'
120+
121+
const MyComponent = defineComponent({
122+
data() {
123+
return { count: 1 }
124+
},
125+
methods: {
126+
increment() {
127+
this.count++
128+
}
129+
}
130+
})
131+
```
132+
133+
## defineAsyncComponent
134+
135+
Creates an async component that will be loaded only when it's necessary.
136+
137+
### Arguments
138+
139+
For basic usage, `defineAsyncComponent` can accept a a factory function returning a `Promise`. Promise's `resolve` callback should be called when you have retrieved your component definition from the server. You can also call `reject(reason)` to indicate the load has failed.
140+
141+
```js
142+
import { defineAsyncComponent } from 'vue'
143+
144+
const AsyncComp = defineAsyncComponent(() =>
145+
import('./components/AsyncComponent.vue')
146+
)
147+
148+
app.component('async-component', AsyncComp)
149+
```
150+
151+
When using [local registration](../guide/component-registration.html#local-registration), you can also directly provide a function that returns a `Promise`:
152+
153+
```js
154+
import { createApp, defineAsyncComponent } from 'vue'
155+
156+
createApp({
157+
// ...
158+
components: {
159+
components: {
160+
AsyncComponent: defineAsyncComponent(() =>
161+
import('./components/AsyncComponent.vue')
162+
)
163+
}
164+
}
165+
})
166+
```
167+
168+
For advanced usage, `defineAsyncComponent` can accept an object:
169+
170+
The `defineAsyncComponent` method can also return an object of the following format:
171+
172+
```js
173+
import { defineAsyncComponent } from 'vue'
174+
175+
const AsyncComp = defineAsyncComponent({
176+
// The factory function
177+
loader: () => import('./Foo.vue')
178+
// A component to use while the async component is loading
179+
loadingComponent: LoadingComponent,
180+
// A component to use if the load fails
181+
errorComponent: ErrorComponent,
182+
// Delay before showing the loading component. Default: 200ms.
183+
delay: 200,
184+
// The error component will be displayed if a timeout is
185+
// provided and exceeded. Default: Infinity.
186+
timeout: 3000,
187+
// A function that returns a boolean indicating whether the async component should retry when the loader promise rejects
188+
retryWhen: error => error.code !== 404,
189+
// Maximum allowed retries number
190+
maxRetries: 3,
191+
// Defining if component is suspensible
192+
suspensible: false
193+
})
194+
```
195+
196+
**See also**: [Dynamic and Async components](../guide/component-dynamic-async.html)
197+
198+
## nextTick
199+
200+
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.
201+
202+
```js
203+
import { createApp, nextTick } from 'vue'
204+
205+
const app = createApp({
206+
setup() {
207+
const message = ref('Hello!')
208+
const changeMessage = async newMessage => {
209+
message.value = newMessage
210+
await nextTick()
211+
console.log('Now DOM is updated')
212+
}
213+
}
214+
})
215+
```
216+
217+
**See also**: [`$nextTick` instance method](instance-methods.html#nexttick)

src/api/instance-methods.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251

252252
- **Usage:**
253253

254-
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. This is the same as the global `Vue.nextTick`, except that the callback's `this` context is automatically bound to the instance calling this method.
254+
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. This is the same as the global `nextTick`, except that the callback's `this` context is automatically bound to the instance calling this method.
255255

256256
- **Example:**
257257

@@ -274,4 +274,4 @@
274274
})
275275
```
276276

277-
- **See also:** [Vue.nextTick](TODO)
277+
- **See also:** [nextTick](global-api.html#nexttick)

src/api/options-composition.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ The `setup` function is a new component option. It serves as the entry point for
221221
</script>
222222
```
223223

224-
Note that [refs](TODO) returned from `setup` are automatically unwrapped when accessed in the template so there's no need for `.value` in templates.
224+
Note that [refs](refs-api.html#ref) returned from `setup` are automatically unwrapped when accessed in the template so there's no need for `.value` in templates.
225225

226226
- **Usage with Render Functions / JSX**
227227

@@ -318,4 +318,4 @@ The `setup` function is a new component option. It serves as the entry point for
318318

319319
- Having `props` as a separate argument makes it easier to type it individually without messing up the types of other properties on the context. It also makes it possible to keep a consistent signature across `setup`, `render` and plain functional components with TSX support.
320320

321-
- **See also:** [Composition API](TODO)
321+
- **See also:** [Composition API](composition-api.html)

src/api/options-dom.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@
6262
The `render` function has priority over the render function compiled from `template` option or in-DOM HTML template of the mounting element
6363
:::
6464

65-
- **See also:** [Render Functions](TODO)
65+
- **See also:** [Render Functions](../guide/render-function.html)

src/guide/component-basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ For example, you might have components for a header, sidebar, and content area,
7575
To use these components in templates, they must be registered so that Vue knows about them. There are two types of component registration: **global** and **local**. So far, we've only registered components globally, using `component` method of created app:
7676

7777
```js
78-
const app = Vue.createApp()
78+
const app = Vue.createApp({})
7979

8080
app.component('my-component-name', {
8181
// ... options ...

src/guide/component-dynamic-async.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const AsyncComp = defineAsyncComponent(() =>
7676
app.component('async-component', AsyncComp)
7777
```
7878

79-
You can also use `defineAsyncComponent` when [registering a component locally](components-registration.html#Local-Registration):
79+
You can also use `defineAsyncComponent` when [registering a component locally](component-registration.html#local-registration):
8080

8181
```js
8282
import { createApp, defineAsyncComponent } from 'vue'
@@ -97,4 +97,4 @@ Async components are _suspensible_ by default. This means if it has a [`<Suspens
9797

9898
The async component can opt-out of `Suspense` control and let the component always control its own loading state by specifying `suspensible: false` in its options.
9999

100-
You can check the list of available options in the [API Reference](TODO)
100+
You can check the list of available options in the [API Reference](../api/global-api.html#arguments-4)

src/guide/custom-directive.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ A directive definition object can provide several hook functions (all optional):
5757
- `beforeUpdate`: called before the containing component's VNode is updated
5858

5959
:::tip Note
60-
We'll cover VNodes in more detail [later](TODO:/render-function.html#The-Virtual-DOM), when we discuss [render functions](TODO:./render-function.html).
60+
We'll cover VNodes in more detail [later](render-function.html#the-virtual-dom-tree), when we discuss render functions.
6161
:::
6262

6363
- `updated`: called after the containing component's VNode **and the VNodes of its children** have updated.
@@ -66,7 +66,7 @@ We'll cover VNodes in more detail [later](TODO:/render-function.html#The-Virtual
6666

6767
- `unmounted`: called only once, when the directive is unbound from the element and the parent component is unmounted.
6868

69-
You can check the arguments passed into these hooks (i.e. `el`, `binding`, `vnode`, and `prevVnode`) in [Custom Directive API](../api/global-api.html#directive)
69+
You can check the arguments passed into these hooks (i.e. `el`, `binding`, `vnode`, and `prevVnode`) in [Custom Directive API](../api/application-api.html#directive)
7070

7171
### Dynamic Directive Arguments
7272

@@ -227,7 +227,7 @@ return withDirectives(h('div'), [[vDemo, test]])
227227

228228
Where `vDemo` will be the directive object written by the user, which contains hooks like `mounted` and `updated`.
229229

230-
`withDirectives` returns a cloned VNode with the user hooks wrapped and injected as VNode lifecycle hooks (see [Render Function](TODO:Render-functions) for more details):
230+
`withDirectives` returns a cloned VNode with the user hooks wrapped and injected as VNode lifecycle hooks (see [Render Function](render-function.html) for more details):
231231

232232
```js
233233
{

src/guide/list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Since it's a generic mechanism for Vue to identify nodes, the `key` also has oth
158158
Don't use non-primitive values like objects and arrays as `v-for` keys. Use string or numeric values instead.
159159
:::
160160

161-
For detailed usage of the `key` attribute, please see the [`key` API documentation](TODO:https://vuejs.org/v2/api/#key).
161+
For detailed usage of the `key` attribute, please see the [`key` API documentation](../api/special-attributes.html#key).
162162

163163
## Array Change Detection
164164

src/guide/mixins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ app.mount('#mixins-global')
160160
// => "hello from component!"
161161
```
162162

163-
In most cases, you should only use it for custom option handling like demonstrated in the example above. It's also a good idea to ship them as [Plugins](TODO) to avoid duplicate application.
163+
In most cases, you should only use it for custom option handling like demonstrated in the example above. It's also a good idea to ship them as [Plugins](plugins.html) to avoid duplicate application.
164164

165165
## Custom Option Merge Strategies
166166

src/guide/render-function.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ Before we dive into render functions, it’s important to know a little about ho
8989
<div>
9090
<h1>My title</h1>
9191
Some text content
92-
<!-- TODO: Add tagline -->
9392
</div>
9493
```
9594

src/guide/state-management.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
Large applications can often grow in complexity, due to multiple pieces of state scattered across many components and the interactions between them. To solve this problem, Vue offers [vuex](https://github.com/vuejs/vuex), our own Elm-inspired state management library. It even integrates into [vue-devtools](https://github.com/vuejs/vue-devtools), providing zero-setup access to [time travel debugging](https://raw.githubusercontent.com/vuejs/vue-devtools/master/media/demo.gif).
66

7-
<!--TODO: use an updated tutorial for 3.0?-->
8-
<div class="vue-mastery"><a href="https://www.vuemastery.com/courses/mastering-vuex/intro-to-vuex/" target="_blank" rel="sponsored noopener" title="Vuex Tutorial">Watch a video explanation on Vue Mastery</a></div>
9-
107
### Information for React Developers
118

129
If you're coming from React, you may be wondering how vuex compares to [redux](https://github.com/reactjs/redux), the most popular Flux implementation in that ecosystem. Redux is actually view-layer agnostic, so it can easily be used with Vue via [simple bindings](https://classic.yarnpkg.com/en/packages?q=redux%20vue&p=1). Vuex is different in that it _knows_ it's in a Vue app. This allows it to better integrate with Vue, offering a more intuitive API and improved development experience.

0 commit comments

Comments
 (0)