You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/introduction.md
+71-62Lines changed: 71 additions & 62 deletions
Original file line number
Diff line number
Diff line change
@@ -46,14 +46,13 @@ At the core of Vue.js is a system that enables us to declaratively render data t
46
46
</div>
47
47
```
48
48
```js
49
-
constapp=newVue({
50
-
el:'#app',
51
-
data() {
52
-
return {
53
-
message:'Hello Vue!'
54
-
}
49
+
constApp= {
50
+
data: {
51
+
message:'Hello Vue!'
55
52
}
56
-
})
53
+
}
54
+
55
+
Vue.createApp().mount(App, '#app')
57
56
```
58
57
<intro-1 />
59
58
@@ -70,14 +69,13 @@ In addition to text interpolation, we can also bind element attributes like this
70
69
</div>
71
70
```
72
71
```js
73
-
constapp2=newVue({
74
-
el:'#app-2',
75
-
data() {
76
-
return {
77
-
message:'You loaded this page on '+newDate().toLocaleString()
78
-
}
72
+
constApp2= {
73
+
data: {
74
+
message:'You loaded this page on '+newDate().toLocaleString()
79
75
}
80
-
})
76
+
}
77
+
78
+
Vue.createApp().mount(App2, '#app-2')
81
79
```
82
80
<intro-2 />
83
81
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:
91
89
<spanv-if="seen">Now you see me</span>
92
90
</div>
93
91
```
92
+
```js
93
+
constApp3= {
94
+
data: {
95
+
message:'You loaded this page on '+newDate().toLocaleString()
96
+
}
97
+
}
98
+
99
+
Vue.createApp().mount(App3, '#app-3')
100
+
```
94
101
<intro-3/>
95
102
96
103
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.
109
116
</div>
110
117
```
111
118
```js
112
-
constapp4=newVue({
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
+
constApp4= {
120
+
data: {
121
+
todos: [
122
+
{ text:'Learn JavaScript' },
123
+
{ text:'Learn Vue' },
124
+
{ text:'Build something awesome' }
125
+
]
122
126
}
123
-
})
127
+
}
128
+
129
+
Vue.createApp().mount(App4, '#app-4')
124
130
```
125
131
<intro-4/>
126
132
@@ -135,19 +141,18 @@ To let users interact with your app, we can use the `v-on` directive to attach e
@@ -162,14 +167,13 @@ Vue also provides the `v-model` directive that makes two-way binding between for
162
167
</div>
163
168
```
164
169
```js
165
-
var app6 =newVue({
166
-
el:'#app-6',
167
-
data() {
168
-
return {
169
-
message:'Hello Vue!'
170
-
}
170
+
constApp6= {
171
+
data: {
172
+
message:'Hello Vue!'
171
173
}
172
-
})
174
+
}
175
+
176
+
Vue.createApp().mount(App6, '#app-6')
173
177
```
174
178
<intro-6/>
175
179
@@ -179,15 +183,21 @@ The component system is another important concept in Vue, because it's an abstra
179
183
180
184

181
185
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:
183
187
184
188
```js
185
189
// Define a new component called todo-item
186
-
Vue.component('todo-item', {
190
+
constTodoItem= {
187
191
template:'<li>This is a todo</li>'
188
-
})
192
+
}
189
193
190
-
constapp=newVue(...)
194
+
constApp= {
195
+
components: {
196
+
'todo-item': TodoItem
197
+
}
198
+
}
199
+
200
+
Vue.createApp().mount(...)
191
201
```
192
202
193
203
Now you can compose it in another component's template:
@@ -202,13 +212,10 @@ Now you can compose it in another component's template:
202
212
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):
203
213
204
214
```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
+
constTodoItem= {
209
216
props: ['todo'],
210
217
template:'<li>{{ todo.text }}</li>'
211
-
})
218
+
}
212
219
```
213
220
214
221
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`:
231
238
</div>
232
239
```
233
240
```js
234
-
Vue.component('todo-item', {
241
+
constTodoItem= {
235
242
props: ['todo'],
236
243
template:'<li>{{ todo.text }}</li>'
237
-
})
238
-
239
-
constapp7=newVue({
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
+
constApp7= {
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' }
0 commit comments