Skip to content

Commit dd7a825

Browse files
NataliaTepluhinayyx990803
authored andcommitted
feat: added lesson 2 description
1 parent f45aeb3 commit dd7a825

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

src/tutorial/src/step-1/description.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Adding Data
1+
# Adding Reactive Properties
2+
3+
:::tip WIP
4+
The tutorial is currently work in progress. Check back later!
5+
:::
26

37
For now, our component renders some static data. Try to change a number 3 in the code to something different to see how the rendered result changes.
48

src/tutorial/src/step-2/App/composition.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ import { ref } from 'vue'
22

33
export default {
44
name: 'App',
5-
setup() {}
5+
setup() {
6+
const colorsNumber = ref(3)
7+
8+
return { colorsNumber }
9+
}
610
}
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export default {
22
name: 'App',
33
data() {
4-
return {}
4+
return {
5+
colorsNumber: 3
6+
}
57
}
68
}

src/tutorial/src/step-2/App/style.css

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
#app {
7+
font-family: Avenir, Helvetica, Arial, sans-serif;
8+
-webkit-font-smoothing: antialiased;
9+
-moz-osx-font-smoothing: grayscale;
10+
text-align: center;
11+
color: #2c3e50;
12+
}
13+
14+
label {
15+
font-size: 18px;
16+
}
17+
18+
input {
19+
font-size: 18px;
20+
padding-left: 5px;
21+
width: 35px;
22+
}
23+
24+
.panel {
25+
color: white;
26+
background-color: teal;
27+
transition: background 0.5s;
28+
}
+6-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
<h1>Step 2!</h1>
1+
<main>
2+
<div class="panel">
3+
<h1>Guess the Color</h1>
4+
<h2>Number of colors: {{ colorsNumber }}</h2>
5+
</div>
6+
</main>
+42-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1-
# Two-way Binding
1+
# Handling User Input
2+
3+
Let's make our example more interactive! What if we could allow user to change `colorsNumber` from the UI? To implement this, we need to start with adding an `<input>` field to our component template
4+
5+
```html{4}
6+
<div class="panel">
7+
<h1>Guess the Color</h1>
8+
<h2>Number of colors: 3</h2>
9+
<input type="number">
10+
</div>
11+
```
12+
13+
Now, we have to provide some initial value to our input, and we want to display `colorsNumber` there. Let's use a dynamic binding to provide a value:
14+
15+
```html
16+
<input type="number" :value="colorsNumber" />
17+
```
18+
19+
However, when we change the number in the input field, we don't see any changes on `Number of colors` in the template 🤔. It happens because we only have _one-way binding_ now, displaying reactive property in the input field but not _changing_ it on user input. Let's listen to the `input` DOM event on the `<input>` field and update `colorsNumber` on it:
20+
21+
```html
22+
<input
23+
type="number"
24+
:value="colorsNumber"
25+
@input="colorsNumber = $event.target.value"
26+
/>
27+
```
28+
29+
Now we created a _two-way binding_: we update `input` field on reactive property change and vice versa. Vue.js has a `v-model` directive to make two-way binding less verbose. Let's replace out `:value` and `@input` with it:
30+
31+
```html
32+
<input type="number" v-model="colorsNumber" />
33+
```
34+
35+
Now, we can choose how many colors we'll have in our guessing game! As a last touch, let's add a label to our input field and set up minimum and maximum for it:
36+
37+
```html
38+
<label for="squares">
39+
Enter number of colors and press Enter:
40+
<input id="squares" type="number" v-model="squaresNumber" min="2" max="10" />
41+
</label>
42+
```

0 commit comments

Comments
 (0)