vite常用配置

news/2024/7/10 0:07:04 标签: vite常用配置, vue

vite基本配置

文章目录

    • vite基本配置
      • 相关文档
      • scss预处理器
      • 样式自动添加前缀
      • vue-router
      • axios
      • vuex
      • vant
      • element-plus
      • vite-plugin-imp
      • alias别名配置
      • vite.config.js一些配置

相关文档

vue3文档

vite中文网


提示

  1. 安装包可通过npmyarn方式安装
  2. 我目前安装的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')
    ]
}

vuerouter_125">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_229">vuex

  • 安装
npm i vuex@next -S


vant

npm i vant@next -S

element-plus

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)
  • 按需引入有以下两种方式:
    1. 使用官网方式 vite-plugin-style-import
    2. 使用vite-plugin-imp 插件
    3. 如果使用按需引入的方式, 在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/, '')
            }
        }
    }
});

http://www.niftyadmin.cn/n/1581753.html

相关文章

uniapp全局或局部配置分享

uniapp自定义全局分享好友 文章目录uniapp自定义全局分享好友创建mixins目录使用全局引入局部引入vue mixins创建mixins目录 在项目根目录下创建mixins目录新建share.js文件 export default {data() {return {shareData: {title: 好东西一起分享~~~,imageUrl: ,},}},onLoad()…

2000-01-01与当前时间的差值,转换UTC时间

2000-01-01与当前时间的差值&#xff0c;转换UTC时间 最近使用微信小程序BLE开发项目&#xff0c;在对接硬件协议过程中&#xff0c;关于UTC时间计算的问题 文章目录2000-01-01与当前时间的差值&#xff0c;转换UTC时间当前时间差值转UTC时间计算使用转为16进制ps&#xff1a; …

uniapp轮播图视频图片组合

uniapp轮播图视频/图片组合 具体根据个人需求修改 文章目录uniapp轮播图视频/图片组合参考文档templatejs参考文档 api template <view class"swiperWrap" v-if"bannerList.length > 0"><swiper class"swiper-box" change"sw…

vue全局使用scss引入mixin、function等函数

vue全局使用scss引入mixin、function等函数 文章目录vue全局使用scss引入mixin、function等函数安装配置新增配置重启服务sass-resources-loaderdesc&#xff1a; 引入全局&#xff0c;就不用每个页面引用… 安装 npm install sass-resources-loader -D配置 vue构建webpack配…

uniapp注册全局过滤器

vue 注册全局过滤器 文章目录vue 注册全局过滤器创建filter文件夹index.js引用文件使用filter创建filter文件夹 在项目根目录创建filter文件夹在该目录下创建index.js文件 index.js /*** 订单状态*/ // export function orderStatus (status) { export const orderStatus (…

vue生成自定义样式二维码

使用vue-qr生成自定义二维码 vue-qr具有自定义二维码背景、logo、实点颜色等特性&#xff0c;能够生成更炫酷的二维码。 文章目录使用vue-qr生成自定义二维码效果图安装使用部分参数说明vue-qr效果图 例&#xff1a;支付生成二维码 安装 npm install vue-qr -S使用 根据需求自…

vue配置代理

vue中配置代理 一直忘记更新… 文章目录vue中配置代理配置代理&#xff0c;解决跨域重启服务请求效果图配置代理&#xff0c;解决跨域 config/index.js module.exports {dev: {// PathsassetsSubDirectory: static,assetsPublicPath: /,// 配置代理proxyTable: { /apis: {targ…

vue全局调用弹窗组件

vue全局调用弹窗组件 使用vue开发pc端商城&#xff0c;检测未登录状态时&#xff0c;在组件/js文件中弹窗提示用户扫码登录功能… 文章目录vue全局调用弹窗组件效果图场景1. 创建vue组件templatejs2. 创建js文件data获取数据效果图3. 注册组件4. 使用组件中调用js文件调用另一种…