第一步 执行命令 npm init vue@latest
第二步.vite项目使用 less sass scss
npm install less less-loader -D
npm install sass node-sass sass-loader -D
yarn add less less-loader
yarn add sass sass-loader node-sass
vite 创建vue3 适配移动端
npm install postcss-px-to-viewport -D
因为vite中已经内联了postcss,所以并不需要额外的创建 postcss.config.js文件
第一步 :vite.config.ts
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import postcsspxtoviewport from 'postcss-px-to-viewport'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
css: {
postcss: {
plugins: [
postcsspxtoviewport({
unitToConvert: 'px', // 要转化的单位
viewportWidth: 750, // UI设计稿的宽度
unitPrecision: 6, // 转换后的精度,即小数点位数
propList: ['*'], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
viewportUnit: 'vw', // 指定需要转换成的视窗单位,默认vw
fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位,默认vw
selectorBlackList: ['ignore-'], // 指定不转换为视窗单位的类名,
minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
replace: true, // 是否转换后直接更换属性值
landscape: false // 是否处理横屏情况
})
]
}
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
})
第二步: 创建声明文件 postcss-px-to-viewport.d.ts
declare module 'postcss-px-to-viewport' {
type Options = {
unitToConvert: 'px' | 'rem' | 'cm' | 'em',
viewportWidth: number,
viewportHeight: number, // not now used; TODO: need for different units and math for different properties
unitPrecision: number,
viewportUnit: string,
fontViewportUnit: string, // vmin is more suitable.
selectorBlackList: string[],
propList: string[],
minPixelValue: number,
mediaQuery: boolean,
replace: boolean,
landscape: boolean,
landscapeUnit: string,
landscapeWidth: number
}
export default function(options: Partial<Options>):any
}
第三步在 tsconfig.config.json 引入声明文件
{
"extends": "@vue/tsconfig/tsconfig.node.json",
"include": ["vite.config.*", "vitest.config.*", "cypress.config.*", "postcss-px-to-viewport.*"],
"compilerOptions": {
"composite": true,
"types": ["node"]
}
}
vscode 小妙招:配置用户代码片段
第一步:点开左边的设置图标,选择配置用户代码片段
第二步: 搜索vue.json
第三步:写配置代码(可以根据自己的需求写,下面是vue3的示例)
{
"Print to console": {
"prefix": "vue3",
"body": [
"<template>",
"",
"<div></div>",
"",
"</template>",
"",
"<script setup lang='ts'>",
"import { ref, reactive } from 'vue'",
"",
"</script>",
"<style scoped>",
"",
"</style>"
],
"description": "Log output to console"
}
}
vscode 插件 json2ts 可以自动生成数据类型
第一步先创建.json 文件, 把数据写进 .json文件里
然后快捷键 ctrl + alt + v 就可自动生成数据类型
安装pnpm: npm install pnpm -g
推荐使用pnpm 下载依赖,可以减少很多用不上的幽灵依赖
vue3+ ts 项目推荐使用的插件
Vue Language Features (Volar)
与vetur
相同,volar
是一个针对vue
的vscode
插件,不过与vetur
不同的是,volar
提供了更为强大的功能(使用的时候需要把vetur 设置为禁用状态)
与他配套的还有 TypeScript Vue Plugin (Volar)