Skip to content

Commit 51e1c0b

Browse files
author
ntepluhina
committed
Fixed all examples to Vue 3
1 parent 1febcd9 commit 51e1c0b

File tree

1 file changed

+71
-62
lines changed

1 file changed

+71
-62
lines changed

docs/guide/introduction.md

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ At the core of Vue.js is a system that enables us to declaratively render data t
4646
</div>
4747
```
4848
``` js
49-
const app = new Vue({
50-
el: '#app',
51-
data() {
52-
return {
53-
message: 'Hello Vue!'
54-
}
49+
const App = {
50+
data: {
51+
message: 'Hello Vue!'
5552
}
56-
})
53+
}
54+
55+
Vue.createApp().mount(App, '#app')
5756
```
5857
<intro-1 />
5958

@@ -70,14 +69,13 @@ In addition to text interpolation, we can also bind element attributes like this
7069
</div>
7170
```
7271
``` js
73-
const app2 = new Vue({
74-
el: '#app-2',
75-
data() {
76-
return {
77-
message: 'You loaded this page on ' + new Date().toLocaleString()
78-
}
72+
const App2 = {
73+
data: {
74+
message: 'You loaded this page on ' + new Date().toLocaleString()
7975
}
80-
})
76+
}
77+
78+
Vue.createApp().mount(App2, '#app-2')
8179
```
8280
<intro-2 />
8381
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."
@@ -91,6 +89,15 @@ It's easy to toggle the presence of an element, too:
9189
<span v-if="seen">Now you see me</span>
9290
</div>
9391
```
92+
```js
93+
const App3 = {
94+
data: {
95+
message: 'You loaded this page on ' + new Date().toLocaleString()
96+
}
97+
}
98+
99+
Vue.createApp().mount(App3, '#app-3')
100+
```
94101
<intro-3/>
95102

96103
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.
@@ -109,18 +116,17 @@ There are quite a few other directives, each with its own special functionality.
109116
</div>
110117
```
111118
``` js
112-
const app4 = new Vue({
113-
el: '#app-4',
114-
data() {
115-
return {
116-
todos: [
117-
{ text: 'Learn JavaScript' },
118-
{ text: 'Learn Vue' },
119-
{ text: 'Build something awesome' }
120-
]
121-
}
119+
const App4 = {
120+
data: {
121+
todos: [
122+
{ text: 'Learn JavaScript' },
123+
{ text: 'Learn Vue' },
124+
{ text: 'Build something awesome' }
125+
]
122126
}
123-
})
127+
}
128+
129+
Vue.createApp().mount(App4, '#app-4')
124130
```
125131
<intro-4/>
126132

@@ -135,19 +141,18 @@ To let users interact with your app, we can use the `v-on` directive to attach e
135141
</div>
136142
```
137143
``` js
138-
constapp5 = new Vue({
139-
el: '#app-5',
140-
data() {
141-
return {
142-
message: 'Hello Vue.js!'
143-
}
144+
const App5 = {
145+
data: {
146+
message: 'Hello Vue.js!'
144147
},
145148
methods: {
146149
reverseMessage() {
147150
this.message = this.message.split('').reverse().join('')
148151
}
149152
}
150-
})
153+
}
154+
155+
Vue.createApp().mount(App5, '#app-5')
151156
```
152157
<intro-5/>
153158

@@ -162,14 +167,13 @@ Vue also provides the `v-model` directive that makes two-way binding between for
162167
</div>
163168
```
164169
``` js
165-
var app6 = new Vue({
166-
el: '#app-6',
167-
data() {
168-
return {
169-
message: 'Hello Vue!'
170-
}
170+
const App6 = {
171+
data: {
172+
message: 'Hello Vue!'
171173
}
172-
})
174+
}
175+
176+
Vue.createApp().mount(App6, '#app-6')
173177
```
174178
<intro-6/>
175179

@@ -179,15 +183,21 @@ The component system is another important concept in Vue, because it's an abstra
179183

180184
![Component Tree](/images/components.png)
181185

182-
In Vue, a component is essentially a Vue instance with pre-defined options. Registering a component in Vue is straightforward:
186+
In Vue, a component is essentially a Vue instance with pre-defined options. Registering a component in Vue is straightforward: we create a component object as we did with `App` objects and we define it in parent's `components` option:
183187

184188
``` js
185189
// Define a new component called todo-item
186-
Vue.component('todo-item', {
190+
const TodoItem = {
187191
template: '<li>This is a todo</li>'
188-
})
192+
}
189193

190-
const app = new Vue(...)
194+
const App = {
195+
components: {
196+
'todo-item': TodoItem
197+
}
198+
}
199+
200+
Vue.createApp().mount(...)
191201
```
192202

193203
Now you can compose it in another component's template:
@@ -202,13 +212,10 @@ Now you can compose it in another component's template:
202212
But this would render the same text for every todo, which is not super interesting. We should be able to pass data from the parent scope into child components. Let's modify the component definition to make it accept a [prop](TODO:components.html#Props):
203213

204214
``` js
205-
Vue.component('todo-item', {
206-
// The todo-item component now accepts a
207-
// "prop", which is like a custom attribute.
208-
// This prop is called todo.
215+
const TodoItem = {
209216
props: ['todo'],
210217
template: '<li>{{ todo.text }}</li>'
211-
})
218+
}
212219
```
213220

214221
Now we can pass the todo into each repeated component using `v-bind`:
@@ -231,23 +238,25 @@ Now we can pass the todo into each repeated component using `v-bind`:
231238
</div>
232239
```
233240
``` js
234-
Vue.component('todo-item', {
241+
const TodoItem = {
235242
props: ['todo'],
236243
template: '<li>{{ todo.text }}</li>'
237-
})
238-
239-
const app7 = new Vue({
240-
el: '#app-7',
241-
data() {
242-
return {
243-
groceryList: [
244-
{ id: 0, text: 'Vegetables' },
245-
{ id: 1, text: 'Cheese' },
246-
{ id: 2, text: 'Whatever else humans are supposed to eat' }
247-
]
248-
}
244+
}
245+
246+
const App7 = {
247+
data: {
248+
groceryList: [
249+
{ id: 0, text: 'Vegetables' },
250+
{ id: 1, text: 'Cheese' },
251+
{ id: 2, text: 'Whatever else humans are supposed to eat' }
252+
]
253+
},
254+
components: {
255+
'todo-item': TodoItem
249256
}
250-
})
257+
}
258+
259+
Vue.createApp().mount(App7, '#app-7')
251260
```
252261
<intro-7/>
253262

0 commit comments

Comments
 (0)