-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVue JS Magic Spells.js
166 lines (107 loc) · 2.63 KB
/
Vue JS Magic Spells.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
_____________________________________________________________________________
Vue JS Magic Spells!
@) How we define Vue.
new Vue({ //this is a vue instance. can be assigned to a variable like vm=new Vue()
el: '#app',
data: {
message: 'Hello Vue.js!'
}
})
@) types of options
data, template, element to mount on, methods, lifecycle callbacks
@) for two way binding
<input v-model="message">
@) FOR loop
<li v-for="todo in todos">
@{{ todo }}
</li>
@) Event hangling
<button v-on:click="function_to_run">Click me!</button>
new Vue({
el: '#app4',
methods: {
function_to_run: function () {
//do whatever you want
}
}
})
##### MORE ######
v-on:click="function_to_run"
v-on:keyup.enter="addTodo"
@) String manipulation
message = message.split('').reverse().join('')
@) Data manipulation
--array push
todos.push({ 'key': 'value' })
@) IF condition
<p v-if="greeting">Hello!</p>
@hooks (we can add custom logic into these)
created
compiled
ready
destroyed
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
_____________________________________________________________________________
#Hello world
1)create a div with id. in that, use templating as needed.
<div id="app">
@{{ message }}
</div>
2) put this script.
*el- select the ODM element to affect
*data- input the variables needed
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!'
}
})
_____________________________________________________________________________
#two way binding
<div id="app2">
<p>@{{ message }}</p>
<input v-model="message"> //using v-model, Vue publishes 'message' everywhere.
</div>
new Vue({
el: '#app2',
data: {
message: 'Hello Vue.js!'
}
})
_____________________________________________________________________________
#rendering a list! For loops
<div id="app3">
<ul>
<li v-for="todo in todos">
@{{ todo }}
</li>
</ul>
</div>
new Vue({
el: '#app3',
data: {
todos: [
'Learn JavaScript' ,
'Learn Vue.js' ,
'Build Something Awesome'
]
}
})
#click (event) handling
<div id="app4">
<p>@{{ message }}</p>
<button v-on:click="reverseMessage">Reverse Message</button>
</div>
new Vue({
el: '#app4',
data: {
message: 'Hello Vue.js!'
},
methods: {
reverseMessage: function () {
this.message = this.message.split('').reverse().join('')
}
}
})