vue3 应用的创建&使用

Zoella Lv3

创建应用

1
npm create vue@latest

通过CDN使用vue

这里使用了提供 npm 包服务的 CDN —— unpkg

1
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

DOM 中的根组件模板

当根组件没有设置 template 选项时,Vue 将自动使用容器的 innerHTML 作为模板

这种方式通常用于此种“无构建步骤”(通过 CDN 使用 vue)的应用程序

1
2
3
<div id="app">
<button @click="count++">{{ count }}</button>
</div>
1
2
3
4
5
6
7
8
9
import { createApp } from 'vue';

const app = createApp({
data() {
return {
count: 0
}
}
}).mount('#app');

全局构建

上述链接使用 全局构建 版本的 Vue,所有顶层 API 都暴露为全局 Vue 对象的属性

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
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">{{ message }}</div>

<script>
const { createApp, ref } = Vue;
/** 1. 创建应用并挂载 */
createApp({
setup() {
const message = ref('Hello world!');
return {
message
};
}
}).mount('#app');

/** 2. 分步骤 */
// // a. 创建应用
// const app = createApp({
// setup() {
// const message = ref('Hello world!');
// return {
// message
// }
// };
// });
// // b. 挂载
// app.mount('#app');
</script>

导入映射表(Import maps)

不使用完整的 CDN URL 引入,而使用 es6 的导入映射表(import maps)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script type="importmap">
{
"imports": {
"vue": "https://unpkg.com/vue@3/dist/vue.global.js"
}
}
</script>
<div id="app">{{ message }}</div>

<script type="module">
import { createApp, ref } from "vue";

createApp({
setup() {
const message = ref("Hello world!");
return {
message
};
}
}).mount('#app');
</script>

拆分模块

将代码拆分为单独的 js 文件以便管理

出于安全原因,es 模块只能通过 http 协议工作,所以需要使用本地的 http 服务器,通过 http 协议提供 index.html。比如:安装 Node.js,在 html 文件所在的文件夹运行 npx serve

1
2
3
4
5
6
7
8
<!-- index.html -->
<div id="app"></div>

<script type="module">
import { createApp } from 'vue';
import MyComponent from './my-component.js';
createApp(MyComponent).mount('#app');
</script>
1
2
3
4
5
6
7
8
9
10
// my-component.js
import { ref } from 'vue';

export default {
setup() {
const count = ref(0);
return { count };
},
template: `<div>count is {{ count }}</div>`
}
  • Title: vue3 应用的创建&使用
  • Author: Zoella
  • Created at : 2023-11-06 11:27:47
  • Updated at : 2023-12-26 11:21:53
  • Link: https://zoella-w.github.io/2023/11/06/1-vue3应用的创建&使用/
  • License: This work is licensed under CC BY-NC-SA 4.0.