基本HTML,及組件 - daniel-qa/Vue GitHub Wiki
- index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Vue 示例</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="./app.js" defer></script>
</head>
<body>
<div id="app"></div>
</body>
</html>
script 要包在 body 中
- app.js
const { createApp } = Vue;
const HelloWorld = {
data() {
return {
message: 'Hello, Vue!'
};
},
created() {
console.log('Component created');
},
mounted() {
console.log('Component mounted');
},
updated() {
console.log('Component updated');
},
destroyed() {
console.log('Component destroyed');
},
template: `
<div>
<p>{{ message }}</p>
</div>
`
};
const app = createApp(HelloWorld);
app.mount('#app');
p.s 適用不同裝置顯示:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
const app = Vue.createApp({})
app.component('site-name', {
props: ['title'],
template: `<h4>{{ title }}</h4>`
})
app.mount('#app')
</script>