Skip to content

Commit a6b2e46

Browse files
committed
more noodling
1 parent cd35b47 commit a6b2e46

File tree

3 files changed

+85
-73
lines changed

3 files changed

+85
-73
lines changed

src/guide/essentials/state-and-reactivity.md

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,51 @@
11
# State and Reactivity
22

3-
## Data Properties
3+
:::tip
4+
This section contains different content for Options API and Composition API. You can toggle between the API styles using the "API Preference" switches at the top of the left sidebar.
5+
:::
46

5-
The `data` option for a component is a function. Vue calls this function as part of creating a new component instance. It should return an object, which Vue will then wrap in its reactivity system and store on the component instance as `$data`. For convenience, any top-level properties of that object are also exposed directly via the component instance:
7+
## Declaring Reactive State
68

79
<div class="options-api">
810

11+
We use the `data` option to declare reactive state of a component. The option value should be a function that returns an object. Vue will call the function when creating a new component instance, and wrap the returned object in its reactivity system. The wrapped object is stored on the component instance as `$data`. For convenience, any top-level properties of that object are also exposed directly on the component instance (`this` in methods and lifecycle hooks):
12+
913
```js
10-
const app = Vue.createApp({
14+
export default {
1115
data() {
12-
return { count: 4 }
13-
}
14-
})
15-
16-
const vm = app.mount('#app')
17-
18-
console.log(vm.$data.count) // => 4
19-
console.log(vm.count) // => 4
20-
21-
// Assigning a value to vm.count will also update $data.count
22-
vm.count = 5
23-
console.log(vm.$data.count) // => 5
16+
return {
17+
count: 1
18+
}
19+
},
2420

25-
// ... and vice-versa
26-
vm.$data.count = 6
27-
console.log(vm.count) // => 6
28-
```
21+
mounted() {
22+
console.log(this.$data.count) // => 1
23+
console.log(this.count) // => 1
2924

30-
</div>
31-
<div class="composition-api">
25+
// Assigning a value to this.count will also update $data.count
26+
this.count = 2
27+
console.log(this.$data.count) // => 2
3228

33-
```js
34-
const app = Vue.createApp({
35-
setup() {
36-
const count = Vue.ref(4)
37-
return { count }
29+
// ... and vice-versa
30+
this.$data.count = 3
31+
console.log(this.count) // => 3
3832
}
39-
})
40-
41-
const vm = app.mount('#app')
33+
}
34+
```
4235

43-
console.log(vm.$data.count) // => 4
44-
console.log(vm.count) // => 4
36+
These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data` function. Where necessary, use `null`, `undefined` or some other placeholder value for properties where the desired value isn't yet available.
4537

46-
// Assigning a value to vm.count will also update $data.count
47-
vm.count = 5
48-
console.log(vm.$data.count) // => 5
38+
It is possible to add a new property directly to the component instance without including it in `data`. However, because this property isn't backed by the reactive `$data` object, it won't automatically be tracked by [Vue's reactivity system](/guide/advanced/reactivity-in-depth.html).
4939

50-
// ... and vice-versa
51-
vm.$data.count = 6
52-
console.log(vm.count) // => 6
53-
```
40+
Vue uses a `$` prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_` for internal properties. You should avoid using names for top-level `data` properties that start with either of these characters.
5441

5542
</div>
5643

57-
These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data` function. Where necessary, use `null`, `undefined` or some other placeholder value for properties where the desired value isn't yet available.
44+
<div class="composition-api">
5845

59-
It is possible to add a new property directly to the component instance without including it in `data`. However, because this property isn't backed by the reactive `$data` object, it won't automatically be tracked by [Vue's reactivity system](/guide/advanced/reactivity-in-depth.html).
6046

61-
Vue uses a `$` prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_` for internal properties. You should avoid using names for top-level `data` properties that start with either of these characters.
47+
48+
</div>
6249

6350
## Methods
6451

@@ -114,18 +101,17 @@ Vue doesn't include built-in support for debouncing or throttling but it can be
114101

115102
In cases where a component is only used once, the debouncing can be applied directly within `methods`:
116103

117-
```vue-html
118-
<script src="https://unpkg.com/[email protected]/lodash.min.js"></script>
119-
<script>
120-
Vue.createApp({
121-
methods: {
122-
// Debouncing with Lodash
123-
click: _.debounce(function() {
124-
// ... respond to click ...
125-
}, 500)
126-
}
127-
}).mount('#app')
128-
</script>
104+
```js
105+
import { debounce } from 'lodash-es'
106+
107+
createApp({
108+
methods: {
109+
// Debouncing with Lodash
110+
click: debounce(function () {
111+
// ... respond to click ...
112+
}, 500)
113+
}
114+
}).mount('#app')
129115
```
130116

131117
However, this approach is potentially problematic for components that are reused because they'll all share the same debounced function. To keep the component instances independent from each other, we can add the debounced function in the `created` lifecycle hook:

src/guide/essentials/template-syntax.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Because `v-bind` is so commonly used, it has a dedicated shorthand syntax:
6060

6161
Attributes that start with `:` may look a bit different from normal HTML, but it is in fact a valid character for attribute names and all Vue-supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is optional, but you will likely appreciate it when you learn more about its usage later.
6262

63-
> From this point on, we will be using the shorthand syntax in code examples, as that's the most common usage for Vue developers.
63+
> For the rest of the guide, we will be using the shorthand syntax in code examples, as that's the most common usage for Vue developers.
6464
6565
### Boolean Attributes
6666

@@ -96,9 +96,9 @@ So far we've only been binding to simple property keys in our templates. But Vue
9696

9797
These expressions will be evaluated as JavaScript in the data scope of the current component instance.
9898

99-
JavaScript expressions can be used in the following positions:
99+
In Vue templates, JavaScript expressions can be used in the following positions:
100100

101-
- Inside text interpolation (mustaches)
101+
- Inside text interpolations (mustaches)
102102
- In the attribute value of any `v-` directive bindings (including shorthands)
103103

104104
### Expressions Only
@@ -153,7 +153,7 @@ Another example is the `v-on` directive, which listens to DOM events:
153153
<a @click="url"> ... </a>
154154
```
155155

156-
Here the argument is the event name to listen to. `v-on` is one of the few directives that also have a corresponding shorthand, with its shorthand character being `@`. We will talk about event handling in more detail too.
156+
Here the argument is the event name to listen to: `click`. `v-on` is one of the few directives that also have a corresponding shorthand, with its shorthand character being `@`. We will talk about event handling in more detail too.
157157

158158
### Dynamic Arguments
159159

@@ -187,7 +187,7 @@ In this example, when `eventName`'s value is `"focus"`, `v-on:[eventName]` will
187187

188188
Dynamic arguments are expected to evaluate to a string, with the exception of `null`. The special value `null` can be used to explicitly remove the binding. Any other non-string value will trigger a warning.
189189

190-
#### Dynamic Argument Expression Constraints
190+
#### Dynamic Argument Syntax Constraints
191191

192192
Dynamic argument expressions have some syntax constraints because certain characters, such as spaces and quotes, are invalid inside HTML attribute names. For example, the following is invalid:
193193

src/guide/quick-start.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
---
2+
aside: deep
3+
---
4+
15
# Quick Start
26

37
## Without Build Tools
48

5-
Vue can be used without a build step in progressive enhancement or relatively simple use cases. Simply copy the following code into an HTML file and open it in your browser:
9+
To get started with Vue without a build step, simply copy the following code into an HTML file and open it in your browser:
610

711
```html
812
<script type="importmap">
@@ -13,6 +17,8 @@ Vue can be used without a build step in progressive enhancement or relatively si
1317
}
1418
</script>
1519

20+
<div id="app">{{ message }}</div>
21+
1622
<script type="module">
1723
import { createApp } from 'vue'
1824
@@ -24,19 +30,45 @@ Vue can be used without a build step in progressive enhancement or relatively si
2430
}
2531
}).mount('#app')
2632
</script>
27-
28-
<div id="app">
29-
<!-- template -->
30-
{{ message }}
31-
</div>
3233
```
3334

34-
The above example uses a browser feature called [Import Maps](https://caniuse.com/import-maps) so that we can use the short `'vue'` import specifier across all code samples in the documentation. Import Maps is currently only available in Chromium-based browsers - if your preferred browser does not support it yet, you can polyfill it with [es-module-shims](https://github.com/guybedford/es-module-shims).
35+
Notice how we can import directly from `'vue'` in our code - this is made possible by the `<script type="importmap">` block, leveraging a browser feature called [Import Maps](https://caniuse.com/import-maps). Import maps are currently only available in Chromium-based browsers, so we recommend using Chrome or Edge during the learning process. If your preferred browser does not support import maps yet, you can polyfill it with [es-module-shims](https://github.com/guybedford/es-module-shims).
36+
37+
You can add entries for other dependencies to the import map - just make sure they point to the ES modules version of the library you intend to use.
3538

36-
:::tip For learning only
39+
:::tip Not for production
3740
The import-maps-based setup is meant for learning only - if you intend to use Vue without build tools in in production, make sure to check out the [Production Deployment Guide](/guide/best-practices/production-deployment.html#without-build-tools).
3841
:::
3942

43+
### Serving over HTTP
44+
45+
As we dive deeper into the guide, we may need to split our code into separate JavaScript files so that they are easier to manage. For example:
46+
47+
```js
48+
import MyComponent from './my-component.js'
49+
```
50+
51+
In order for this to work, you need to serve your HTML over the `http://` protocol instead of `file://` protocol. To start a local HTTP server, first install [Node.js](https://nodejs.org/en/), and then run `npx serve` from the command line in the same directory where your HTML file is.
52+
53+
### Using the Global Build
54+
55+
For consistency, we will be exclusively using [ES modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) syntax throughout the documentation. That said, Vue is also available in a more traditional build where it exposes a global `Vue` variable:
56+
57+
```html
58+
<script src="https://unpkg.com/vue@3"></script>
59+
60+
<div id="app">...</div>
61+
62+
<script>
63+
// instead of `import { createApp } from 'vue'`:
64+
const { createApp } = Vue
65+
66+
createApp({
67+
// ...
68+
}).mount('#app')
69+
</script>
70+
```
71+
4072
## With Build Tools
4173

4274
A build setup allows us to use Vue [Single File Components](/guide/scaling-up/sfc) (SFCs). The official Vue build setup is based on [Vite](https://vitejs.dev), a frontend build tool that is modern, lightweight and extremely fast.
@@ -78,17 +110,11 @@ If you are unsure about an option, simply choose `No` by hitting enter for now.
78110

79111
You should now have your first Vue project running!
80112

113+
- The recommended IDE setup is [Visual Studio Code](https://code.visualstudio.com/) + [Volar extension](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar). [WebStorm](https://www.jetbrains.com/webstorm/) is also viable.
81114
- More tooling details are discussed in the [Tooling Guide](/guide/scaling-up/tooling.html).
82115
- To learn more about the underlying build tool Vite, check out the [Vite docs](https://vitejs.dev).
83116
- If you chose to use TypeScript, check out [Using Vue with TypeScript](scaling-up/typescript.html).
84117

85-
## IDE Setup
86-
87-
If you are using Vue with build tools, it is recommended to use the following IDE setup for get syntax highlighting, type inference and auto-completion support for Single File Components:
88-
89-
- Recommended: [Visual Studio Code](https://code.visualstudio.com/) + [Volar extension](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar)
90-
- Also viable: [JetBrains WebStorm](https://www.jetbrains.com/webstorm/)
91-
92118
## Next Steps
93119

94120
If you skipped the [Introduction](/guide/introduction), we strongly recommend reading it before moving on to the rest of the documentation.

0 commit comments

Comments
 (0)