Skip to content

Commit 35f5b52

Browse files
docs: reduce the reliance on the global Vue in API Reference examples (#861)
1 parent 32b9fde commit 35f5b52

11 files changed

+56
-40
lines changed

src/api/application-config.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Every Vue application exposes a `config` object that contains the configuration settings for that application:
44

55
```js
6-
const app = Vue.createApp({})
6+
const app = createApp({})
77

88
console.log(app.config)
99
```
@@ -73,7 +73,7 @@ This can replace Vue 2.x `Vue.prototype` extending:
7373
Vue.prototype.$http = () => {}
7474

7575
// After
76-
const app = Vue.createApp({})
76+
const app = createApp({})
7777
app.config.globalProperties.$http = () => {}
7878
```
7979

@@ -107,7 +107,7 @@ This config option is only respected when using the runtime compiler. If you are
107107
- **Usage:**
108108

109109
```js
110-
const app = Vue.createApp({
110+
const app = createApp({
111111
mounted() {
112112
console.log(this.$options.hello)
113113
}

src/api/built-in-components.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
```
8989

9090
```js
91-
const app = Vue.createApp({
91+
const app = createApp({
9292
...
9393
methods: {
9494
transitionComplete (el) {

src/api/global-api.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@ sidebarDepth: 1
44

55
# Global API
66

7+
If you're using a CDN build then the functions of the global API are accessible via the global `Vue` object. e.g.:
8+
9+
```js
10+
const { createApp, h, nextTick } = Vue
11+
```
12+
13+
If you're using ES modules then they can be imported directly:
14+
15+
```js
16+
import { createApp, h, nextTick } from 'vue'
17+
```
18+
19+
Global functions that handle reactivity, such as `reactive` and `ref`, are documented separately. See [Reactivity API](/api/reactivity-api.html) for those functions.
20+
721
## createApp
822

923
Returns an application instance which provides an application context. The entire component tree mounted by the application instance share the same context.
1024

1125
```js
12-
const app = Vue.createApp({})
26+
const app = createApp({})
1327
```
1428

1529
You can chain other methods after `createApp`, they can be found in [Application API](./application-api.html)
@@ -19,7 +33,7 @@ You can chain other methods after `createApp`, they can be found in [Application
1933
The function receives a root component options object as a first parameter:
2034

2135
```js
22-
const app = Vue.createApp({
36+
const app = createApp({
2337
data() {
2438
return {
2539
...
@@ -34,7 +48,7 @@ const app = Vue.createApp({
3448
With the second parameter, we can pass root props to the application:
3549

3650
```js
37-
const app = Vue.createApp(
51+
const app = createApp(
3852
{
3953
props: ['username']
4054
},
@@ -68,7 +82,7 @@ Returns a returns "virtual node", usually abbreviated to **VNode**: a plain obje
6882
6983
```js
7084
render() {
71-
return Vue.h('h1', {}, 'Some title')
85+
return h('h1', {}, 'Some title')
7286
}
7387
```
7488
@@ -231,7 +245,7 @@ Allows resolving a `component` by its name, if it is available in the current ap
231245
Returns a `Component` or `undefined` when not found.
232246

233247
```js
234-
const app = Vue.createApp({})
248+
const app = createApp({})
235249
app.component('MyComponent', {
236250
/* ... */
237251
})
@@ -296,7 +310,7 @@ Allows resolving a `directive` by its name, if it is available in the current ap
296310
Returns a `Directive` or `undefined` when not found.
297311

298312
```js
299-
const app = Vue.createApp({})
313+
const app = createApp({})
300314
app.directive('highlight', {})
301315
```
302316

src/api/instance-methods.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
- **Example:**
2121

2222
```js
23-
const app = Vue.createApp({
23+
const app = createApp({
2424
data() {
2525
return {
2626
a: 1,
@@ -62,7 +62,7 @@
6262
When watched value is an object or array, any changes to its properties or elements won't trigger the watcher because they reference the same object/array:
6363

6464
```js
65-
const app = Vue.createApp({
65+
const app = createApp({
6666
data() {
6767
return {
6868
article: {
@@ -104,7 +104,7 @@
104104
`$watch` returns an unwatch function that stops firing the callback:
105105

106106
```js
107-
const app = Vue.createApp({
107+
const app = createApp({
108108
data() {
109109
return {
110110
a: 1
@@ -213,7 +213,7 @@
213213
```
214214

215215
```js
216-
const app = Vue.createApp({
216+
const app = createApp({
217217
methods: {
218218
sayHi() {
219219
console.log('Hi!')
@@ -242,7 +242,7 @@
242242
```
243243

244244
```js
245-
const app = Vue.createApp({
245+
const app = createApp({
246246
methods: {
247247
showAdvice(advice) {
248248
alert(advice)
@@ -293,7 +293,7 @@
293293
- **Example:**
294294

295295
```js
296-
Vue.createApp({
296+
createApp({
297297
// ...
298298
methods: {
299299
// ...

src/api/instance-properties.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
The instantiation options used for the current component instance. This is useful when you want to include custom properties in the options:
4242

4343
```js
44-
const app = Vue.createApp({
44+
const app = createApp({
4545
customOption: 'foo',
4646
created() {
4747
console.log(this.$options.customOption) // => 'foo'
@@ -102,14 +102,15 @@
102102
```
103103

104104
```js
105-
const app = Vue.createApp({})
105+
const { createApp, h } = Vue
106+
const app = createApp({})
106107

107108
app.component('blog-post', {
108109
render() {
109-
return Vue.h('div', [
110-
Vue.h('header', this.$slots.header()),
111-
Vue.h('main', this.$slots.default()),
112-
Vue.h('footer', this.$slots.footer())
110+
return h('div', [
111+
h('header', this.$slots.header()),
112+
h('main', this.$slots.default()),
113+
h('footer', this.$slots.footer())
113114
])
114115
}
115116
})

src/api/options-assets.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
- **Usage:**
1212
```js
13-
const app = Vue.createApp({})
14-
13+
const app = createApp({})
14+
1515
app.component('focused-input', {
1616
directives: {
1717
focus: {
@@ -39,8 +39,8 @@
3939
const Foo = {
4040
template: `<div>Foo</div>`
4141
}
42-
43-
const app = Vue.createApp({
42+
43+
const app = createApp({
4444
components: {
4545
Foo
4646
},

src/api/options-composition.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
}
2121

22-
Vue.createApp({
22+
createApp({
2323
created() {
2424
console.log(2)
2525
},

src/api/options-data.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
const data = { a: 1 }
2222

2323
// The object is added to a component instance
24-
const vm = Vue.createApp({
24+
const vm = createApp({
2525
data() {
2626
return data
2727
}
@@ -59,7 +59,7 @@
5959
- **Example:**
6060

6161
```js
62-
const app = Vue.createApp({})
62+
const app = createApp({})
6363

6464
// simple syntax
6565
app.component('props-demo-simple', {
@@ -107,7 +107,7 @@
107107
- **Example:**
108108

109109
```js
110-
const app = Vue.createApp({
110+
const app = createApp({
111111
data() {
112112
return { a: 1 }
113113
},
@@ -152,7 +152,7 @@
152152
- **Example:**
153153

154154
```js
155-
const app = Vue.createApp({
155+
const app = createApp({
156156
data() {
157157
return { a: 1 }
158158
},
@@ -182,7 +182,7 @@
182182
- **Example:**
183183

184184
```js
185-
const app = Vue.createApp({
185+
const app = createApp({
186186
data() {
187187
return {
188188
a: 1,
@@ -262,7 +262,7 @@
262262
- **Usage:**
263263

264264
```js
265-
const app = Vue.createApp({})
265+
const app = createApp({})
266266

267267
// Array syntax
268268
app.component('todo-item', {

src/api/options-dom.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
```
4040

4141
```js
42-
const app = Vue.createApp({})
42+
const { createApp, h } = Vue
43+
const app = createApp({})
4344

4445
app.component('my-title', {
4546
render() {
46-
return Vue.h(
47+
return h(
4748
'h1', // tag name,
4849
this.blogTitle // tag content
4950
)

src/api/options-lifecycle-hooks.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
4242

4343
- **Details:**
4444

45-
Called after the instance has been mounted, where element, passed to `Vue.createApp({}).mount()` is replaced by the newly created `vm.$el`. If the root instance is mounted to an in-document element, `vm.$el` will also be in-document when `mounted` is called.
45+
Called after the instance has been mounted, where element, passed to [`app.mount`](/api/application-api.html#mount) is replaced by the newly created `vm.$el`. If the root instance is mounted to an in-document element, `vm.$el` will also be in-document when `mounted` is called.
4646

4747
Note that `mounted` does **not** guarantee that all child components have also been mounted. If you want to wait until the entire view has been rendered, you can use [vm.$nextTick](../api/instance-methods.html#nexttick) inside of `mounted`:
4848

@@ -186,7 +186,7 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
186186
```
187187

188188
```js
189-
const app = Vue.createApp({
189+
const app = createApp({
190190
data() {
191191
return {
192192
cart: 0
@@ -232,7 +232,7 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
232232
```
233233

234234
```js
235-
const app = Vue.createApp({
235+
const app = createApp({
236236
data() {
237237
return {
238238
cart: 0

src/api/options-misc.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
- **Details:**
88

9-
Allow the component to recursively invoke itself in its template. Note that when a component is registered globally with `Vue.createApp({}).component({})`, the global ID is automatically set as its name.
9+
Allow the component to recursively invoke itself in its template. Note that when a component is registered globally with [`app.component`](/api/application-api.html#component), the global ID is automatically set as its name.
1010

1111
Another benefit of specifying a `name` option is debugging. Named components result in more helpful warning messages. Also, when inspecting an app in the [vue-devtools](https://github.com/vuejs/vue-devtools), unnamed components will show up as `<AnonymousComponent>`, which isn't very informative. By providing the `name` option, you will get a much more informative component tree.
1212

@@ -27,7 +27,7 @@
2727
- **Example:**
2828

2929
```js
30-
Vue.createApp({
30+
createApp({
3131
// Delimiters changed to ES6 template string style
3232
delimiters: ['${', '}']
3333
})

0 commit comments

Comments
 (0)