-
Notifications
You must be signed in to change notification settings - Fork 9
/
vue3.html
112 lines (95 loc) · 2.69 KB
/
vue3.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue3</title>
</head>
<body>
<div id="app"></div>
<script type="module">
import {
createApp,
reactive,
computed,
watch,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated
} from './vue/vue3.js';
const HelloComponent = {
emits: ['greet'],
setup(props, context) {
const state = reactive({
firstName: 'John',
lastName: 'Doe'
});
const fullName = computed(() => state.firstName + ' ' + state.lastName);
onUpdated(() => context.emit('greet', state.firstName));
return {
state,
fullName
};
},
template: `
<div>
<h1>{{ fullName }}</h1>
<slot></slot>
</div>
`
};
const app = createApp({
el: '#app',
components: {
HelloComponent
},
setup(props, context) {
const state = reactive({
message: 'Hello, Vue!',
inputValue: 'ChatGPT'
});
watch(
() => state.message,
(newValue, oldValue) => {
console.log('Message changed:', oldValue, ' -> ', newValue);
}
);
watch(
() => state.inputValue,
(newValue, oldValue) => {
console.log('InputValue changed:', oldValue, ' -> ', newValue);
}
);
const greetMessage = (message) => context.emit('greet', message);
const updateMessage = (newMessage) => (state.message = newMessage);
onBeforeMount(() => console.log('beforeMount hook'));
onMounted(() => console.log('mounted hook'));
onBeforeUpdate(() => console.log('beforeUpdate hook'));
onUpdated(() => console.log('updated hook'));
return {
state,
greetMessage,
updateMessage
};
},
template: `
<div>
<HelloComponent v-on:greet="greetMessage">
<p>{{ state.message }}</p>
</HelloComponent>
<input v-model="state.inputValue" type="text">
<p v-text="state.inputValue"></p>
</div>
`
});
app.$on('greet', (message) => {
console.log('Greet:', message);
});
app.state.inputValue = 'OpenAI';
app.HelloComponent.state.firstName = 'Tom';
app.updateMessage('Hello, World!');
window.app = app;
</script>
</body>
</html>