Open xccjk opened 2 years ago
在安装了@vitejs/plugin-legacy
版本2.x后,运行npm run build进行打包,报错:
Cannot destructure property 'renderBuiltUrl' of 'config.experimental' as it is undefined
原因:plugin-legacy版本为2.x后,vite版本需要为3.x版本
"devDependencies": {
"@vitejs/plugin-legacy": "^2.1.0",
"vite": "^3.1.0",
...
}
按需引入意义:减小打包后的包体积
// main.js
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import zhCn from 'element-plus/lib/locale/lang/zh-cn';
...
app.use(ElementPlus, { locale: zhCn });
<template>
<div class="app">
<el-button>按钮</el-button>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
// 局部引入
import { ElButton } from 'element-plus'
import 'element-plus/theme-chalk/el-button.css'
import 'element-plus/theme-chalk/base.css'
export default defineComponent({
components: { ElButton },
setup() {
return {}
}
})
</script>
<style lang="less"></style>
unplugin-auto-import
和unplugin-vue-components
yarn add unplugin-auto-import unplugin-vue-components -D
vite.config.js
文件import { defineConfig } from 'vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
export default defineConfig({
...
plugins: [
...
AutoImport({
resolvers: [ElementPlusResolver({ importStyle: true })],
}),
Components({
resolvers: [ElementPlusResolver({ importStyle: true })],
}),
],
});
App.vue
设置国际化<template>
<ElConfigProvider :locale="locale">
<router-view />
</ElConfigProvider>
</template>
<script>
import { defineComponent } from 'vue'
import { ElConfigProvider } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
export default defineComponent({
components: {
ElConfigProvider,
},
setup() {
return {
locale: zhCn,
}
},
})
</script>
<style lang="less"></style>
大功告成
vite
打包后的项目在有的浏览器上报错globalThis is not defined
@vitejs/plugin-legacy 2.x版本,需要vite3.x版本
资料