Skip to content

Commit 1177278

Browse files
authored
small edits and legiblity updates (#76)
1 parent a8b6a15 commit 1177278

File tree

4 files changed

+84
-82
lines changed

4 files changed

+84
-82
lines changed

src/guide/computed.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -97,55 +97,7 @@ computed: {
9797

9898
In comparison, a method invocation will **always** run the function whenever a re-render happens.
9999

100-
Why do we need caching? Imagine we have an expensive computed property **A**, which requires looping through a huge array and doing a lot of computations. Then we may have other computed properties that in turn depend on **A**. Without caching, we would be executing **A**’s getter many more times than necessary! In cases where you do not want caching, use a method instead.
101-
102-
### Computed vs Watched Property
103-
104-
Vue does provide a more generic way to observe and react to data changes on a Vue instance: **watch properties**. When you have some data that needs to change based on some other data, it is tempting to overuse `watch` - especially if you are coming from an AngularJS background. However, it is often a better idea to use a computed property rather than an imperative `watch` callback. Consider this example:
105-
106-
```html
107-
<div id="demo">{{ fullName }}</div>
108-
```
109-
110-
```js
111-
const vm = Vue.createApp({
112-
data() {
113-
return {
114-
firstName: 'Foo',
115-
lastName: 'Bar',
116-
fullName: 'Foo Bar'
117-
}
118-
},
119-
watch: {
120-
firstName(val) {
121-
this.fullName = val + ' ' + this.lastName
122-
},
123-
lastName(val) {
124-
this.fullName = this.firstName + ' ' + val
125-
}
126-
}
127-
}).mount('#demo')
128-
```
129-
130-
The above code is imperative and repetitive. Compare it with a computed property version:
131-
132-
```js
133-
const vm = Vue.createApp({
134-
data() {
135-
return {
136-
firstName: 'Foo',
137-
lastName: 'Bar'
138-
}
139-
},
140-
computed: {
141-
fullName() {
142-
return this.firstName + ' ' + this.lastName
143-
}
144-
}
145-
}).mount('#demo')
146-
```
147-
148-
Much better, isn't it?
100+
Why do we need caching? Imagine we have an expensive computed property `list`, which requires looping through a huge array and doing a lot of computations. Then we may have other computed properties that in turn depend on `list`. Without caching, we would be executing `list`’s getter many more times than necessary! In cases where you do not want caching, use a `method` instead.
149101

150102
### Computed Setter
151103

@@ -239,3 +191,51 @@ Result:
239191
In this case, using the `watch` option allows us to perform an asynchronous operation (accessing an API) and sets a condition for performing this operation. None of that would be possible with a computed property.
240192

241193
In addition to the `watch` option, you can also use the imperative [vm.\$watch API](TODO:../api/#vm-watch).
194+
195+
### Computed vs Watched Property
196+
197+
Vue does provide a more generic way to observe and react to data changes on a Vue instance: **watch properties**. When you have some data that needs to change based on some other data, it is tempting to overuse `watch` - especially if you are coming from an AngularJS background. However, it is often a better idea to use a computed property rather than an imperative `watch` callback. Consider this example:
198+
199+
```html
200+
<div id="demo">{{ fullName }}</div>
201+
```
202+
203+
```js
204+
const vm = Vue.createApp({
205+
data() {
206+
return {
207+
firstName: 'Foo',
208+
lastName: 'Bar',
209+
fullName: 'Foo Bar'
210+
}
211+
},
212+
watch: {
213+
firstName(val) {
214+
this.fullName = val + ' ' + this.lastName
215+
},
216+
lastName(val) {
217+
this.fullName = this.firstName + ' ' + val
218+
}
219+
}
220+
}).mount('#demo')
221+
```
222+
223+
The above code is imperative and repetitive. Compare it with a computed property version:
224+
225+
```js
226+
const vm = Vue.createApp({
227+
data() {
228+
return {
229+
firstName: 'Foo',
230+
lastName: 'Bar'
231+
}
232+
},
233+
computed: {
234+
fullName() {
235+
return this.firstName + ' ' + this.lastName
236+
}
237+
}
238+
}).mount('#demo')
239+
```
240+
241+
Much better, isn't it?

src/guide/instance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Every Vue application starts by creating a new **Vue instance** with the `create
88
Vue.createApp(/* options */)
99
```
1010

11-
After the Vue instance is created, we can _mount_ it, passing a container to `.mount` method. For example, if we want to mount a Vue application on `<div id="app"></div>`, we should pass `#app`:
11+
After the Vue instance is created, we can _mount_ it, passing a container to `mount` method. For example, if we want to mount a Vue application on `<div id="app"></div>`, we should pass `#app`:
1212

1313
```js
1414
Vue.createApp(/* options */).mount('#app')

src/guide/introduction.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Vue.createApp(AttributeBindingApp).mount('#bind-attribute')
8383
</p>
8484
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
8585

86-
Here we are encountering something new. The `v-bind` attribute you are seeing is called a **directive**. Directives are prefixed with `v-` to indicate that they are special attributes provided by Vue, and as you may have guessed, they apply special reactive behavior to the rendered DOM. Here, it is basically saying "keep this element's `title` attribute up-to-date with the `message` property on the Vue instance."
86+
Here we're encountering something new. The `v-bind` attribute you're seeing is called a **directive**. Directives are prefixed with `v-` to indicate that they are special attributes provided by Vue, and as you may have guessed, they apply special reactive behavior to the rendered DOM. Here we are basically saying "_keep this element's `title` attribute up-to-date with the `message` property on the Vue instance._"
8787

8888
## Conditionals and Loops
8989

@@ -109,7 +109,7 @@ Vue.createApp(ConditionalRenderingApp).mount('#conditional-rendering')
109109

110110
This example demonstrates that we can bind data to not only text and attributes, but also the **structure** of the DOM. Moreover, Vue also provides a powerful transition effect system that can automatically apply [transition effects](TODO) when elements are inserted/updated/removed by Vue.
111111

112-
You can change `seen` from `true` to `false` in the sandbox below to check the effect
112+
You can change `seen` from `true` to `false` in the sandbox below to check the effect:
113113

114114
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="js,result" data-user="Vue" data-slug-hash="oNXdbpB" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Conditional rendering">
115115
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/oNXdbpB">
@@ -118,7 +118,7 @@ You can change `seen` from `true` to `false` in the sandbox below to check the e
118118
</p>
119119
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
120120

121-
There are quite a few other directives, each with its own special functionality. For example, the `v-for` directive can be used for displaying a list of items using the data from an Array:
121+
There are quite a few other directives, each with its own special functionality. For example, the `v-for` directive can be used to display a list of items using the data from an Array:
122122

123123
```html
124124
<div id="list-rendering">

src/guide/template-syntax.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ These expressions will be evaluated as JavaScript in the data scope of the owner
8383
{{ if (ok) { return message } }}
8484
```
8585

86-
::: tip
87-
Template expressions are sandboxed and only have access to a [whitelist of globals](TODO:https://github.com/vuejs/vue/blob/v2.6.10/src/core/instance/proxy.js#L9) such as `Math` and `Date`. You should not attempt to access user defined globals in template expressions.</p>
88-
:::
89-
9086
## Directives
9187

9288
Directives are special attributes with the `v-` prefix. Directive attribute values are expected to be **a single JavaScript expression** (with the exception of `v-for`, which will be discussed later). A directive's job is to reactively apply side effects to the DOM when the value of its expression changes. Let's review the example we saw in the introduction:
@@ -137,31 +133,6 @@ Similarly, you can use dynamic arguments to bind a handler to a dynamic event na
137133

138134
In this example, when `eventName`'s value is `"focus"`, `v-on:[eventName]` will be equivalent to `v-on:focus`.
139135

140-
#### Dynamic Argument Value Constraints
141-
142-
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.
143-
144-
#### Dynamic Argument Expression Constraints
145-
146-
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:
147-
148-
```html
149-
<!-- This will trigger a compiler warning. -->
150-
<a v-bind:['foo' + bar]="value"> ... </a>
151-
```
152-
153-
The workaround is to either use expressions without spaces or quotes, or replace the complex expression with a computed property.
154-
155-
When using in-DOM templates (templates directly written in an HTML file), you should also avoid naming keys with uppercase characters, as browsers will coerce attribute names into lowercase:
156-
157-
```html
158-
<!--
159-
This will be converted to v-bind:[someattr] in in-DOM templates.
160-
Unless you have a "someattr" property in your instance, your code won't work.
161-
-->
162-
<a v-bind:[someAttr]="value"> ... </a>
163-
```
164-
165136
### Modifiers
166137

167138
Modifiers are special postfixes denoted by a dot, which indicate that a directive should be bound in some special way. For example, the `.prevent` modifier tells the `v-on` directive to call `event.preventDefault()` on the triggered event:
@@ -203,3 +174,34 @@ The `v-` prefix serves as a visual cue for identifying Vue-specific attributes i
203174
```
204175

205176
They may look a bit different from normal HTML, but `:` and `@` are valid characters 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 totally optional, but you will likely appreciate it when you learn more about its usage later.
177+
178+
### Caveats
179+
180+
#### Dynamic Argument Value Constraints
181+
182+
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.
183+
184+
#### Dynamic Argument Expression Constraints
185+
186+
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:
187+
188+
```html
189+
<!-- This will trigger a compiler warning. -->
190+
<a v-bind:['foo' + bar]="value"> ... </a>
191+
```
192+
193+
We recommend replacing any complex expressions with a [computed property](computed.html), one of the most fundamental pieces of Vue, which we'll cover shortly.
194+
195+
When using in-DOM templates (templates directly written in an HTML file), you should also avoid naming keys with uppercase characters, as browsers will coerce attribute names into lowercase:
196+
197+
```html
198+
<!--
199+
This will be converted to v-bind:[someattr] in in-DOM templates.
200+
Unless you have a "someattr" property in your instance, your code won't work.
201+
-->
202+
<a v-bind:[someAttr]="value"> ... </a>
203+
```
204+
205+
#### JavaScript Expressions
206+
207+
Template expressions are sandboxed and only have access to a [whitelist of globals](TODO:https://github.com/vuejs/vue/blob/v2.6.10/src/core/instance/proxy.js#L9) such as `Math` and `Date`. You should not attempt to access user defined globals in template expressions.

0 commit comments

Comments
 (0)