-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.2outsideInstance.js
98 lines (89 loc) · 1.81 KB
/
09.2outsideInstance.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
// Store in a variable if you want access to your instance from outside
var vm1 = new Vue({
// Reference of html
el: '#app1',
// Object
data: {
title: 'The VueJS Instance',
showParagraph: false
},
methods: {
show: function() {
this.showParagraph = true;
this.updateTitle('The VueJS Instance (Updated)');
console.log(this.$refs);
},
updateTitle: function(title) {
this.title = title;
}
},
computed: {
lowercaseTitle: function() {
return this.title.toLowerCase();
}
},
watch: {
title: function(value) {
alert('Title changed, new value: ' + value);
}
}
});
console.log(vm1);
// We can't add new properties outside the instance
// vm1.newPro = "new";
// But we can change it
setTimeout(function(){
vm1.title = 'Change by Timer';
vm1.show();
},3000);
var vm2 = new Vue({
el: '#app2',
data: {
title: 'The second Instance',
showParagraph: false
},
methods:{
onChange: function(){
vm1.title = 'Changed!'
}
}
})
// Find data natural form data
var data = {
title: 'The third Instance'
}
var vm3 = new Vue({
el: '#app3',
data: data,
methods:{
refExample: function(){
this.$refs.myButton.innerText = 'Modificando por $ref'
}
}
})
console.log(vm3.$data === data);
// Si no le pongo defino el 'el' lo puedo definir despues.
var vm4 = new Vue({
data: {
title: 'The fourth Instance',
}
});
vm4.$mount('#app4');
// Add html by vue
var vm5 = new Vue({
template: '<h1>Hello!</h1>'
});
// Opcion 1
vm5.$mount('#app5');
// Opcion 2
// vm5.$mount();
// document.getElementById('app5').appendChild(vm5.$el);
// Directamente aplicando a clase, tag,..
var vm6 = new Vue({
el: 'tag',
template: '<h1>By tag!</h1>'
});
// var vm6 = new Vue({
// el: '.class',
// template: '<h1>By class!</h1>'
// });