-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
75 lines (67 loc) · 1.93 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue.js Start</title>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
margin-top: 60px;
}
</style>
</head>
<body>
<h1>Vue.js</h1>
<p>is ready to start an adventure</p>
<div id="app">
<h2>My awesome list</h2>
<ul>
<!--1/ 2. Then, we're using built-in v-for directive to iterate over products -->
<li v-for="(p, index) in products">
<span>{{ p.name }}</span>
<button v-on:click="removeItem(index)">x</button>
</li>
</ul>
<!--1/ 5. v-if can be helpful with conditional statements -->
<p v-if="!products.length">No products!</p>
<!--1/ 6. v-on adds an handler and :click is the name of the event, then goes the function to invoke -->
<button v-on:click="removeItem(products.length - 1)">Remove last item</button>
<button v-on:click="addItem('Tea')">Add hardcoded</button>
<form v-on:submit.prevent="onSubmit()">
<input v-model="newItemName" type="text">
<button type="submit">Add item</button>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
//7/ 1. We can define any JavaScript some array of objects
products: [],
counter: 0,
newItemName: ''
},
//1/ 3. To get some events from user we can define a method in 'methods'
methods: {
removeItem(index) {
this.products.splice(index, 1);
},
addItem(name) {
this.products.push({
id: this.counter++,
name
});
},
onSubmit() {
this.addItem(this.newItemName);
this.newItemName = '';
}
}
})
</script>
</body>
</html>