Skip to content

Commit b64f3b5

Browse files
authored
Merge pull request #40 from vuejs/fix-create-app
Fix createApp and mount usage
2 parents 1761311 + f184e25 commit b64f3b5

File tree

10 files changed

+316
-350
lines changed

10 files changed

+316
-350
lines changed

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

src/guide/class-and-style.md

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ You can have multiple classes toggled by having more fields in the object. In ad
2828
And the following data:
2929

3030
```js
31-
data: {
32-
isActive: true,
33-
hasError: false
31+
data() {
32+
return {
33+
isActive: true,
34+
hasError: false
35+
}
3436
}
3537
```
3638

@@ -49,10 +51,12 @@ The bound object doesn't have to be inline:
4951
```
5052

5153
```js
52-
data: {
53-
classObject: {
54-
active: true,
55-
'text-danger': false
54+
data() {
55+
return {
56+
classObject: {
57+
active: true,
58+
'text-danger': false
59+
}
5660
}
5761
}
5862
```
@@ -64,9 +68,11 @@ This will render the same result. We can also bind to a [computed property](comp
6468
```
6569

6670
```js
67-
data: {
68-
isActive: true,
69-
error: null
71+
data() {
72+
return {
73+
isActive: true,
74+
error: null
75+
}
7076
},
7177
computed: {
7278
classObject() {
@@ -87,9 +93,11 @@ We can pass an array to `v-bind:class` to apply a list of classes:
8793
```
8894

8995
```js
90-
data: {
91-
activeClass: 'active',
92-
errorClass: 'text-danger'
96+
data() {
97+
return {
98+
activeClass: 'active',
99+
errorClass: 'text-danger'
100+
}
93101
}
94102
```
95103

@@ -122,9 +130,11 @@ When you use the `class` attribute on a custom component, those classes will be
122130
For example, if you declare this component:
123131

124132
```js
125-
const MyComponent = {
126-
template: '<p class="foo bar">Hi!</p>'
127-
}
133+
const app = Vue.createApp()
134+
135+
app.component('my-component', {
136+
template: `<p class="foo bar">Hi!</p>`
137+
})
128138
```
129139

130140
Then add some classes when using it:
@@ -135,17 +145,6 @@ Then add some classes when using it:
135145
</div>
136146
```
137147

138-
```js
139-
Vue.createApp().mount(
140-
{
141-
components: {
142-
'my-component': MyComponent
143-
}
144-
},
145-
'#app'
146-
)
147-
```
148-
149148
The rendered HTML will be:
150149

151150
```html
@@ -177,9 +176,11 @@ The object syntax for `v-bind:style` is pretty straightforward - it looks almost
177176
```
178177

179178
```js
180-
data: {
181-
activeColor: 'red',
182-
fontSize: 30
179+
data() {
180+
return {
181+
activeColor: 'red',
182+
fontSize: 30
183+
}
183184
}
184185
```
185186

@@ -190,10 +191,12 @@ It is often a good idea to bind to a style object directly so that the template
190191
```
191192

192193
```js
193-
data: {
194-
styleObject: {
195-
color: 'red',
196-
fontSize: '13px'
194+
data() {
195+
return {
196+
styleObject: {
197+
color: 'red',
198+
fontSize: '13px'
199+
}
197200
}
198201
}
199202
```

src/guide/computed.md

Lines changed: 68 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ In-template expressions are very convenient, but they are meant for simple opera
1414

1515
At this point, the template is no longer simple and declarative. You have to look at it for a second before realizing that it displays `message` in reverse. The problem is made worse when you want to include the reversed message in your template more than once.
1616

17-
That's why for any complex logic, you should use a **computed property**.
17+
That's why for complex logic that includes reactive data, you should use a **computed property**.
1818

1919
### Basic Example
2020

@@ -26,26 +26,23 @@ That's why for any complex logic, you should use a **computed property**.
2626
```
2727

2828
```js
29-
const vm = Vue.createApp().mount(
30-
{
31-
data() {
32-
return {
33-
message: 'Hello'
34-
}
35-
},
36-
computed: {
37-
// a computed getter
38-
reversedMessage() {
39-
// `this` points to the vm instance
40-
return this.message
41-
.split('')
42-
.reverse()
43-
.join('')
44-
}
29+
const vm = Vue.createApp({
30+
data() {
31+
return {
32+
message: 'Hello'
4533
}
4634
},
47-
'#example'
48-
)
35+
computed: {
36+
// a computed getter
37+
reversedMessage() {
38+
// `this` points to the vm instance
39+
return this.message
40+
.split('')
41+
.reverse()
42+
.join('')
43+
}
44+
}
45+
}).mount('#example')
4946
```
5047

5148
Result:
@@ -106,47 +103,41 @@ Vue does provide a more generic way to observe and react to data changes on a Vu
106103
```
107104

108105
```js
109-
const vm = Vue.createApp().mount(
110-
{
111-
data() {
112-
return {
113-
firstName: 'Foo',
114-
lastName: 'Bar',
115-
fullName: 'Foo Bar'
116-
}
117-
},
118-
watch: {
119-
firstName(val) {
120-
this.fullName = val + ' ' + this.lastName
121-
},
122-
lastName(val) {
123-
this.fullName = this.firstName + ' ' + val
124-
}
106+
const vm = Vue.createApp({
107+
data() {
108+
return {
109+
firstName: 'Foo',
110+
lastName: 'Bar',
111+
fullName: 'Foo Bar'
125112
}
126113
},
127-
'#demo'
128-
)
114+
watch: {
115+
firstName(val) {
116+
this.fullName = val + ' ' + this.lastName
117+
},
118+
lastName(val) {
119+
this.fullName = this.firstName + ' ' + val
120+
}
121+
}
122+
}).mount('#demo')
129123
```
130124

131125
The above code is imperative and repetitive. Compare it with a computed property version:
132126

133127
```js
134-
const vm = Vue.createApp().mount(
135-
{
136-
data() {
137-
return {
138-
firstName: 'Foo',
139-
lastName: 'Bar'
140-
}
141-
},
142-
computed: {
143-
fullName() {
144-
return this.firstName + ' ' + this.lastName
145-
}
128+
const vm = Vue.createApp({
129+
data() {
130+
return {
131+
firstName: 'Foo',
132+
lastName: 'Bar'
146133
}
147134
},
148-
'#demo'
149-
)
135+
computed: {
136+
fullName() {
137+
return this.firstName + ' ' + this.lastName
138+
}
139+
}
140+
}).mount('#demo')
150141
```
151142

152143
Much better, isn't it?
@@ -199,38 +190,35 @@ For example:
199190
<!-- gives you the freedom to use what you're familiar with. -->
200191
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
201192
<script>
202-
const watchExampleVM = Vue.createApp().mount(
203-
{
204-
data() {
205-
return {
206-
question: '',
207-
answer: 'Questions usually contain a question mark. ;-)'
208-
}
209-
},
210-
watch: {
211-
// whenever question changes, this function will run
212-
question(newQuestion, oldQuestion) {
213-
if (newQuestion.indexOf('?') > -1) {
214-
this.getAnswer()
215-
}
216-
}
217-
},
218-
methods: {
219-
getAnswer() {
220-
this.answer = 'Thinking...'
221-
axios
222-
.get('https://yesno.wtf/api')
223-
.then(response => {
224-
this.answer = _.capitalize(response.data.answer)
225-
})
226-
.catch(error => {
227-
this.answer = 'Error! Could not reach the API. ' + error
228-
})
193+
const watchExampleVM = Vue.createApp({
194+
data() {
195+
return {
196+
question: '',
197+
answer: 'Questions usually contain a question mark. ;-)'
198+
}
199+
},
200+
watch: {
201+
// whenever question changes, this function will run
202+
question(newQuestion, oldQuestion) {
203+
if (newQuestion.indexOf('?') > -1) {
204+
this.getAnswer()
229205
}
230206
}
231207
},
232-
'#watch-example'
233-
)
208+
methods: {
209+
getAnswer() {
210+
this.answer = 'Thinking...'
211+
axios
212+
.get('https://yesno.wtf/api')
213+
.then(response => {
214+
this.answer = _.capitalize(response.data.answer)
215+
})
216+
.catch(error => {
217+
this.answer = 'Error! Could not reach the API. ' + error
218+
})
219+
}
220+
}
221+
}).mount('#watch-example')
234222
</script>
235223
```
236224

0 commit comments

Comments
 (0)