https://blog.csdn.net/LuckyWinty/article/details/106293997
当后端接口还没有完成的时候,前端没必要等。可以自己使用 mock 来构建服务端接口的环境和数据。自己动手,丰衣足食。
安装
pnpm add mockjs vite-plugin-mock -D
配置
在 vite.config.ts 中配置
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { defineConfig } from 'vite'
import { viteMockServe } from 'vite-plugin-mock'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
viteMockServe({
supportTs: true,
mockPath: "./src/mock/source", // 解析,路径可根据实际变动
localEnabled: true, // 开发环境设为true,
prodEnabled: false, // 生产环境设为true,也可以根据官方文档格式
injectCode:
` import { setupProdMockServer } from './src/mock';
setupProdMockServer(); `,
watchFiles: true, // 监听文件内容变更
injectFile: resolve("src/main.ts"), // 在m
})
]
})
新建数据
在 src/mock/source 下新建 user.ts 文件,内容如下:
export default [
{
url: "/api/users",
method: "GET",
response: () => {
return {
code: 0,
message: "success",
result: {
nickname: '神奇动物在哪里',
gender: 1
},
};
}
}
]
使用
import axios from 'axios'
axios.create({
baseURL: import.meta.env.VITE_BASE_URL
})
axios.get("/api/users").then(res => { console.log(res); });
运行起来,看到控制台的网络中请求的状况:
请求网址: http://localhost:3000/api/users
请求方法: GET
状态代码: 200 OK
远程地址: 127.0.0.1:3000
引荐来源网址政策: strict-origin-when-cross-origin
# response
{code: 0, message: "success", result: {nickname: "神奇动物在哪里", gender: 1}}
code: 0
message: "success"
result: {nickname: "神奇动物在哪里", gender: 1}
gender: 1
nickname: "神奇动物在哪里"
原子化 css 的好用是真的好用。
https://antfu.me/posts/reimagine-atomic-css-zh
https://juejin.cn/post/7028841960752283656
https://blog.csdn.net/qq_41499782/article/details/124074678
https://blog.csdn.net/sg_knight/article/details/124097860
https://github.com/unocss/unocss
使用 unocss
https://github.com/unocss/unocss
class 可以参考参考
https://www.tailwindcss.cn/docs/align-items
这里用 pnpm 来安装。
- 安装添加
pnpm add unocss -D
- 添加到 vite.config.ts 中
import vue from '@vitejs/plugin-vue'
import Unocss from 'unocss/vite'
import { defineConfig } from 'vite'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
Unocss({})
]
})
- main.ts 中引入
import 'uno.css'
- 项目中使用
<div class="w-300px h-120px rounded-8px m-20px shadow-md">
</div>
仅仅用样式就可以实现宽300像素高120像素圆角8像素 margin 20 像素,带 md 程度的阴影的div盒子。这样多方便呀。
当你已经在使用了,可以通过 http://localhost:3000/__unocss 查看生成的 css 文件,是不是很人性化,文件量还小。
vue 面试题
都说 pnpm vite vue 组合好。那就试试。
# shenya 是项目名
pnpm create vite shenya
cd shenya
pnpm install
pnpm run dev
如果从来没有使用过 pnpm,第一次创建的时候是要花一点时间。后边就是秒创建了哈。
vue cli 创建项目的时候,默认是不会 .env .env.production 这些文件的。一般,一个大型项目是需要用到不同环境的不同配置。这个时候,手动建立各个 .env 文件是必要的。起点就在 package.json 中的 scripts 里,默认就会有的。当然你可以手动定义名字,然后用 mode 指定。如:"staging": "vue-cli-service serve --mode staging" 。
https://cli.vuejs.org/zh/guide/mode-and-env.html
如果是 vite 环境,不用 process,而是用 import.meta。 会有一些变化。请看:
vue 项目中,出现 Component name "home" should always be multi-word.eslint 这个错误提示,可以改两个地方。
处理方式
修改 vue.config.js 文件,在 defineConfig 中加入 lintOnSave: false ,这个保证编译不会报错。
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
lintOnSave: false
})
修改 .eslintrc.js 文件,在其中追加配置 overrides 。注意自己的 vue 文件目录配置好。这个保证在 vue 文件中,不会出现警告的波浪线。有强迫症的受不了。
module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/vue3-essential',
'eslint:recommended',
'@vue/typescript/recommended'
],
parserOptions: {
ecmaVersion: 2020
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
},
overrides: [
{
files: ['src/views/**/*.vue'],
rules: {
'vue/multi-word-component-names': 0,
}
}
]
}
升级 node,vue-cli 到最新版本
node -v
# 更新 node 到最新版本
sudo n stable
# 更新 pnpm
pnpm add -g pnpm
# 更新 yarn
yarn set version latest
# pnpm 安装 @vue/cli
pnpm install @vue/cli -g
# 或使用 yarn 安装
yarn global add @vue/cli
# 创建项目,指定包管理工具
vue create -m pnpm demo
创建 vue 项目
这里就建一个 ts 的,使用 sass 的 vue3.x 项目,使用手动选择模式,包管理工具使用 yarn 。一路选择和回车即可:
vue create -m yarn zeipan
- step-1
? Please pick a preset:
Default ([Vue 3] babel, eslint)
Default ([Vue 2] babel, eslint)
❯ Manually select features
- step-2
? Check the features needed for your project: (Press <space> to select, <a> to toggle all, <i> to invert selection, and <enter> to proceed)
◉ Babel
◉ TypeScript
◯ Progressive Web App (PWA) Support
◯ Router
◯ Vuex
❯◉ CSS Pre-processors
◉ Linter / Formatter
◯ Unit Testing
◯ E2E Testing
- step-3
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, TS, CSS Pre-processors, Linter
? Choose a version of Vue.js that you want to start the project with (Use arrow keys)
❯ 3.x
2.x
- step-4
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, TS, CSS Pre-processors, Linter
? Choose a version of Vue.js that you want to start the project with 3.x
? Use class-style component syntax? No
? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): (Use arrow keys)
❯ Sass/SCSS (with dart-sass)
Less
Stylus
一路选择回车后,提示安装成功。进入项目,运行项目。
cd zeipan
yarn serve
ui 框架
这个风格最喜欢
这个组件功能最丰富
https://github.com/qingqingxuan/admin-work
上边两个都是基于第一个的 ui 库实现
https://www.naiveui.com/zh-CN/os-theme/docs/community
https://primefaces.org/primevue/
大前端组合方案
vue3 + ts + vite
丢掉 vue2 丢掉 js 丢掉 webpack
当然 react 栈另说
在使用 <style scoped>
,我们可以保护其他元素不受该样式的影响。如果我们附加动态元素,我们应该使用 parent-class /deep/ current-class
。 但是我如何通过SCSS使用它呢:
<style scoped lang="scss">
::v-deep .frame {
}
</style>