vite常用配置
vite基本配置文章目录vite基本配置相关文档scss预处理器样式自动添加前缀vue-routeraxiosvuexvantelement-plusvite-plugin-impalias别名配置vite.config.js一些配置相关文档vue3文档vite中文网提示:安装包可通过npm 或 yarn方式安装我目前安装的vite版本是^2.1.5, 配置仅供参考scss预处理器无需下载node
·
vite基本配置
文章目录
相关文档
提示:
- 安装包可通过
npm
或yarn
方式安装 - 我目前安装的
vite
版本是^2.1.5
, 配置仅供参考
scss预处理器
- 无需下载
node-sass、 sass-loader
包, 直接安装sass
- 同样的
less、 stylus
也是直接安装
// .scss and .sass
npm i sass -D
// .less
npm i less -D
// .styl and .stylus
npm i stylus -D
- 引入全局
scss
样式, 在vite.config.js
中配置
preprocessorOptions: {
scss: {
additionalData: "@import './src/assets/style/mixin.scss';"
}
}
- 使用
<style lang="scss" scoped>
img {
width: $width;
}
#wrap {
width: calc(100% - #{$width});
}
</style>
样式自动添加前缀
- 安装
npm install autoprefixer postcss -D
- 可以在
vite.config.js
文件中配置 或 新建postcss.config.js
配置文件, 配置如下
// vite.config.js 配置
css: {
postcss: {
plugins: [
require('autoprefixer')
]
}
},
// postcss.config.js
module.exports = {
plugins: [
require('autoprefixer')
]
}
vue-router
- 安装 vue-router
npm install vue-router@4 -S
- 在
App.vue
中 将组件映射到路由上
<template>
<router-view></router-view>
</template>
- 定义路由组件, 新建
router
文件夹下为index.js
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
// 定义路由
const routes = [
{
path: '/',
redirect: '/index'
},
{
path: '/index',
name: 'index',
component: () => import('@/view/index.vue')
}
]
// 创建路由实例并传递 `routes` 配置
const router = createRouter({
// 内部提供了 history 模式的实现
// history: createWebHistory(),
history: createWebHashHistory(),
routes
})
export default router;
- 创建并挂载根实例, 在
main.js
中
// 1. 引入router文件
import router from '@/router'
// 2. use路由
const app = createApp(App);
app.use(router);
app.mount('#app');
// 注意: 把原来创建项目生成注释
// createApp(App).mount('#app')
- 正常,应用可以正常访问了。
注意:
-
routes
引入组件路径时,必须要有.vue
后缀, 否则报错。 -
查看
vue-router
版本:npm info vue-router versions
axios
- 安装
npm i axios -S
vuex
- 安装
npm i vuex@next -S
vant
npm i vant@next -S
element-plus
- 安装
npm install element-plus -S
- 在
main.js
中引入【此方式是完整引入
】
import ElementPlus from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
app.use(ElementPlus)
- 按需引入有以下两种方式:
- 使用官网方式 vite-plugin-style-import
- 使用
vite-plugin-imp
插件 - 如果使用
按需引入
的方式, 在main.js
中 无需引入整个Element Plus
及样式文件。
vite-plugin-imp
一个自动导入组件库样式的vite插件。
- 安装
npm i vite-plugin-imp -D
vite.config.js
文件配置
import vitePluginImp from 'vite-plugin-imp'
plugins: [
vue(),
vitePluginImp({
libList: [
{
libName: 'vant',
style(name) {
if (/CompWithoutStyleFile/i.test(name)) {
// This will not import any style file
return false
}
return `vant/es/${name}/style/index.js`
}
},
{
libName: 'element-plus',
style: (name) => {
return`element-plus/lib/theme-chalk/${name}.css`
}
}
]
})
],
- 使用
<template>
<div class="indexWrap">
<Progress percentage="15"></Progress>
<Button type="warning">vant button</Button>
</div>
</template>
<script>
import { Progress, Button } from 'vant'
export default {
components: {
Button,
Progress
},
}
</script>
alias别名配置
- 在
vite.config.js
配置
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
'views': path.resolve(__dirname, 'src/view'),
}
},
// 也可以使用数组方式
resolve: {
alias: [{
find: '@',
replacement: path.resolve(__dirname, 'src')
},
{
find: 'views',
replacement: path.resolve(__dirname, 'src/view')
}
],
},
- 使用, 通过
别名/
import router from '@/router'
vite.config.js一些配置
export default defineConfig({
plugins: [
vue(),
vitePluginImp({
libList: [
{
libName: 'vant',
style(name) {
if (/CompWithoutStyleFile/i.test(name)) {
// This will not import any style file
return false
}
return `vant/es/${name}/style/index.js`
}
},
{
libName: 'element-plus',
style: (name) => {
return`element-plus/lib/theme-chalk/${name}.css`
}
}
]
})
],
base: './',
// 静态资源服务的文件夹,默认public
publicDir: 'public',
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
},
extensions: ['.js', '.vue', '.json', '.scss', '*']
},
css: {
// 引入 autoprefixer
postcss: {
plugins: [
require('autoprefixer')
]
},
// 引入全局 scss
preprocessorOptions: {
scss: {
additionalData: "@import './src/assets/style/mixin.scss';"
}
}
},
server: {
// 服务器主机名,如果允许外部访问,可设置为"0.0.0.0"
host: '0.0.0.0',
port: 56438, // 服务器端口号
open: false, // 是否自动打开浏览器
// 代理
proxy: {
'/api': {
target: 'http://xxx.xxx.xx',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
}
});
更多推荐
已为社区贡献9条内容
所有评论(0)