Skip to content

Commit cd35b47

Browse files
committed
wip: application & template syntax
1 parent ce406a3 commit cd35b47

23 files changed

+235
-109
lines changed

.github/contributing/writing-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Writing documentation is an exercise in empathy. We're not describing an objecti
5757

5858
- **Avoid abbreviations** in writing and code examples (e.g. `attribute` is better than `attr`, `message` is better than `msg`), unless you are specifically referencing an abbreviation in an API (e.g. `$attrs`). Abbreviation symbols included on standard keyboards (e.g. `@`, `#`, `&`) are OK.
5959
- **When referencing a directly following example, use a colon (`:`) to end a sentence**, rather than a period (`.`).
60-
- **Use the Oxford comma** (e.g. "a, b, and c" instead of "a, b and c"). ![Why the Oxford comma is important](/images/oxford-comma.jpg)
60+
- **Use the Oxford comma** (e.g. "a, b, and c" instead of "a, b and c"). ![Why the Oxford comma is important](./oxford-comma.jpg)
6161
- Source: [The Serial (Oxford) Comma: When and Why To Use It](https://www.inkonhand.com/2015/10/the-serial-oxford-comma-when-and-why-to-use-it/)
6262
- **When referencing the name of a project, use the name that project refers to itself as.** For example, "webpack" and "npm" should both use lowercase as that's how their documentation refers to them.
6363
- **Use Title Case for headings** - at least for now, since it's what we use through the rest of the docs. There's research suggesting that sentence case (only first word of the heading starts with a capital) is actually superior for legibility and also reduces the cognitive overhead for documentation writers, since they don't have to try to remember whether to capitalize words like "and", "with", and "about".
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
;(() => {
2-
const restore = (key, cls) => {
2+
const restore = (key, cls, def = false) => {
33
const saved = localStorage.getItem(key)
4-
if (saved && saved !== 'false') {
4+
if (saved ? saved !== 'false' : def) {
55
document.documentElement.classList.add(cls)
66
}
77
}
88
restore('vue-docs-prefer-composition', 'prefer-composition')
9-
restore('vue-docs-prefer-sfc', 'prefer-sfc')
9+
restore('vue-docs-prefer-sfc', 'prefer-sfc', true)
1010
})()
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { ref } from 'vue'
22

33
const hasStorage = typeof localStorage !== 'undefined'
4-
const get = (key: string): boolean =>
5-
hasStorage ? JSON.parse(localStorage.getItem(key) || 'false') : false
4+
const get = (key: string, defaultValue = false): boolean =>
5+
hasStorage
6+
? JSON.parse(localStorage.getItem(key) || String(defaultValue))
7+
: defaultValue
68

79
export const preferCompositionKey = 'vue-docs-prefer-composition'
810
export const preferComposition = ref(get(preferCompositionKey))
911

1012
export const preferSFCKey = 'vue-docs-prefer-sfc'
11-
export const preferSFC = ref(get(preferSFCKey))
13+
export const preferSFC = ref(get(preferSFCKey, true))

src/.vitepress/theme/styles/inline-demo.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
.demo {
2-
background-color: var(--vt-c-bg-soft);
2+
/* background-color: var(--vt-c-bg-soft); */
3+
transition: border-color 0.5s;
34
padding: 22px 24px;
45
border-radius: 8px;
6+
border: 1px solid var(--vt-c-divider-light);
7+
}
8+
9+
.demo p {
10+
margin: 0;
511
}
612

713
.demo button {

src/api/application.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## createApp()
44

5+
// TODO rework this
6+
// TODO document that you can pass props to the root component
7+
58
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:
69

710
```js
@@ -347,7 +350,7 @@ You can modify its properties, listed below, before mounting your application.
347350
- **Usage:**
348351

349352
```js
350-
app.config.errorHandler = (err, vm, info) => {
353+
app.config.errorHandler = (err, instance, info) => {
351354
// handle error
352355
// `info` is a Vue-specific error info, e.g. which lifecycle hook
353356
// the error was found in
@@ -367,7 +370,7 @@ Assign a handler for uncaught errors during component render function and watche
367370
- **Usage:**
368371

369372
```js
370-
app.config.warnHandler = function (msg, vm, trace) {
373+
app.config.warnHandler = function (msg, instance, trace) {
371374
// `trace` is the component hierarchy trace
372375
}
373376
```

src/api/built-in-directives.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@
259259

260260
When used without an argument, can be used to bind an object containing attribute name-value pairs. Note in this mode `class` and `style` does not support Array or Objects.
261261

262+
// TODO details on auto prop/attr check
263+
262264
- **Example:**
263265

264266
```vue-html

src/examples/src/attribute-bindings/App/template.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<p>
22
<span :title="message">
3-
Hover your mouse over me for a few seconds to see my dynamically bound
4-
title!
3+
Hover your mouse over me for a few seconds to see my dynamically bound title!
54
</span>
65
</p>
76

src/guide/essentials/application.md

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## The App Instance
44

5-
Every Vue application starts by creating a new **application instance** with the `createApp` function:
5+
Every Vue application starts by creating a new **application instance** with the [`createApp`](/api/application#createapp) function:
66

77
```js
88
import { createApp } from 'vue'
@@ -14,11 +14,48 @@ const app = createApp({
1414

1515
## The Root Component
1616

17-
## Mounting the App
17+
The object we are passing into `createApp` is in fact a component. Every app requires a "root component" that can contain other components as its children.
18+
19+
If you are using Single File Components, we typically import the root component from another file:
20+
21+
```js
22+
import { createApp } from 'vue'
23+
// import the root component App from a single-file component.
24+
import App from './App.vue'
25+
26+
const app = createApp(App)
27+
```
28+
29+
While many examples in this guide only need a single component, most real applications are organized into a tree of nested, reusable components. For example, a Todo application's component tree might look like this:
30+
31+
```
32+
App (root component)
33+
├─ TodoList
34+
│ └─ TodoItem
35+
│ ├─ DeleteTodoButton
36+
│ └─ EditTodoButton
37+
└─ TodoListFooter
38+
├─ ClearTodosButton
39+
└─ TodoListStatistics
40+
```
41+
42+
We will discuss how to define and compose multiple components together in later sections of the guide. Before that, we will focus on what happens inside a single component.
1843

19-
## App-Scoped Assets
44+
## App Configurations
2045

21-
The application instance is used to register 'globals' that can then be used by components within that application. We'll discuss that in detail later in the guide but as a quick example:
46+
The app instance exposes a `.config` object that allows us to configure a few app-level options, for example defining an app-level error handler that captures errors from all descendent components:
47+
48+
```js
49+
app.config.errorHandler = (err) => {
50+
/* handle error */
51+
}
52+
```
53+
54+
You can browse the full list of `app.config` options in the [API reference](/api/application#app-config).
55+
56+
## Global Assets
57+
58+
The app instance is used to register "global assets" that can then be used by components within that app. We'll discuss them in detail later in the guide but here is a quick example:
2259

2360
```js
2461
const app = createApp({})
@@ -36,9 +73,7 @@ app.provide('store', myStore)
3673
app.use(LocalePlugin)
3774
```
3875

39-
### Chaining
40-
41-
Most of the methods exposed by the application instance return that same instance, allowing for chaining:
76+
Most of the methods exposed by the application instance return that same instance, so we can also use the chaining syntax:
4277

4378
```js
4479
const app = createApp({})
@@ -48,6 +83,61 @@ const app = createApp({})
4883
.use(LocalePlugin)
4984
```
5085

51-
You can browse the full application API in the [API reference](/api/application.html).
86+
You can browse the full list of app instance methods in the [API reference](/api/application).
87+
88+
## Mounting the App
89+
90+
An app instance won't render anything until its `.mount()` method is called.
91+
It expects a "container" argument, which can either be an actual DOM element or a selector string:
92+
93+
```html
94+
<div id="app"></div>
95+
```
96+
97+
```js
98+
app.mount('#app')
99+
```
100+
101+
The content of the app's root component will be rendered inside the container element. The container element itself is not considered part of the app.
102+
103+
The `.mount()` method should always be called after all app configurations and asset registrations are done. Also note that its return value, unlike the asset registration methods, is the root component instance instead of the app instance.
104+
105+
### In-DOM Root Component Template
106+
107+
When using Vue without a build step, we can write our root component's template directly inside the mount container:
108+
109+
```html
110+
<div id="app">
111+
<button @click="count++">{{ count }}</button>
112+
</div>
113+
```
114+
115+
```js
116+
import { createApp } from 'vue'
117+
118+
const app = createApp({
119+
data() {
120+
return {
121+
count: 0
122+
}
123+
}
124+
})
125+
126+
app.mount('#app')
127+
```
128+
129+
Vue will automatically use the container's `innerHTML` as the template if the root component does not already have a `template` option.
52130

53131
## Multiple App Instances
132+
133+
You are not limited to a single app instance on the same page. The `createApp` API allows multiple Vue applications to co-exist on the same page, each with its own scope for configuration and global assets:
134+
135+
```js
136+
const app1 = createApp({ /* ... */ })
137+
app1.mount('#container-1')
138+
139+
const app2 = createApp({ /* ... */ })
140+
app2.mount('#container-2')
141+
```
142+
143+
If you are using Vue to enhance server-rendered HTML and only need Vue to control specific parts of a large page, avoid mounting a singe Vue app instance on the entire page. Instead, create multiple small app instances and mount them on the elements they are responsible for.

0 commit comments

Comments
 (0)