Vue3-Day1

Vue3-Day1

先安装NVM

使用VSCode需要安装的插件

Vue-Official

之后使用NVM命令安装NPM

1
nvm install latest

之后就可以基于 Vite 的构建项目,并进行设置

1
npm create vue@latest

会看到如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
✔ Project name: … <your-project-name>
✔ Add TypeScript? … No / Yes
✔ Add JSX Support? … No / Yes
✔ Add Vue Router for Single Page Application development? … No / Yes
✔ Add Pinia for state management? … No / Yes
✔ Add Vitest for Unit testing? … No / Yes
✔ Add an End-to-End Testing Solution? … No / Cypress / Nightwatch / Playwright
✔ Add ESLint for code quality? … No / Yes
✔ Add Prettier for code formatting? … No / Yes
✔ Add Vue DevTools 7 extension for debugging? (experimental) … No / Yes

Scaffolding project in ./<your-project-name>...
Done.

安装一个Vue3语法插件

1
npm install vite-plugin-vue-setup-extend -D

修改vite.config.ts文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import {fileURLToPath, URL} from 'node:url'

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// 这里添加
import vueSetupExtend from 'vite-plugin-vue-setup-extend'

// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
// 这里添加
vueSetupExtend()
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
},
},
})

只用在setup的script里面写一个name即可,不需要写两个script了

Vue3组合式语法(非响应式数据)

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
<template>
<div class="person">
<h2>{{ a }}</h2>
<h2>name:{{ name }}</h2>
<h2>age:{{ age }}</h2>
<button @click="changeName">修改名字</button>
<button @click="changeAge">修改年龄</button>
<button @click="showTel">tel</button>
</div>

</template>

<script setup lang="ts">
let a = 666
// data
let name = 'zhangsan'
let age = 18
let tel = 13812341234

// function
function changeName() {
name = 'lisi'
}

function changeAge() {
age += 1
}

function showTel() {
alert(tel)
}
</script>


<style>
.person {
background-color: skyblue;
box-shadow: 0 0 10px;
border-radius: 10px;
padding: 20px;
}

button {
margin: 0 5px;
}
</style>

Vue3-Day1
http://example.com/2024/11/13/Vue3-Day1/
Author
J1aHe
Posted on
November 13, 2024
Licensed under