当前位置:   article > 正文

vitejs跨域配置_vite跨域配置

vite跨域配置

新建vite.config.js

import type { UserConfig, ConfigEnv  } from "vite";
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { loadEnv } from 'vite';
// import basicSsl from '@vitejs/plugin-basic-ssl' 如果是https 需要引入

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

export default ({command, mode}: ConfigEnv): UserConfig => {
  const root = process.cwd()

  const env = loadEnv(mode, root)

  return {
    base: env.VITE_PUBLIC_PATH,
    root,
    server: {
      host: '0.0.0.0',
      port: 3000,
      open: true,
      strictPort: false,
      https: fasle, // https 设置为true
      proxy: {
        '/api': {
          target: 'your host', // http://192.168.1.1:20/
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '') // 路径重写
        }
      }
    },
    resolve: {
      alias: [
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/'
        }
      ]
    },
    build: {
      watch: {},
      manifest: true
    },
    plugins: [
    	vue(),
      	// basicSsl(), // https 打开此配置
    ]
  }
}
  • 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
  • 47
  • 48
  • 49
  • 50
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/598092
推荐阅读
  

闽ICP备14008679号