主题
创建一个应用
CDN
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue3 App</title>
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.min.js"></script>
<script>
const { createApp } = Vue
createApp({
template: `
<div>
<h1>{{ count }}</h1>
<button @click="increment">+1</button>
</div>
`,
setup() {
const count = Vue.ref(0)
function increment() {
count.value++
}
return {
count,
increment,
}
},
}).mount('#app')
</script>
</body>
</html>html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue2 App</title>
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.min.js"></script>
<script>
new Vue({
el: '#app',
template: `
<div>
<h1>{{ count }}</h1>
<button @click="increment">+1</button>
</div>
`,
data() {
return {
count: 0,
}
},
methods: {
increment() {
this.count++
},
},
})
</script>
</body>
</html>Vite(Vue 3 为例)
bash
npm init vue@latest底层为
Vite:
- v8 以前:
esbuild(开发阶段) +rollup(生产阶段)- v8 及以后:
rolldown统一
html
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>js
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.mount('#app')vue
<template>
<h1>Hello world</h1>
</template>Vue CLI(Vue 2 为例)
底层为
webpack
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title><%= webpackConfig.name %></title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>js
import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
new Vue({
el: '#app',
render: (h) => h(App),
})vue
<template>
<div>
<h1>{{ count }}</h1>
<button @click="increment">+1</button>
</div>
</template>
<script>
export default {
data() {
return {
count: 0,
}
},
methods: {
increment() {
this.count++
},
},
}
</script>