Vite按需引入elementplus

小白开始写自己的第一个项目,结果在引入element库就频频受挫otz

总结一下遇到的问题:

npm i element-ui -S报错

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: funfund@0.0.0
npm ERR! Found: vue@3.4.21
npm ERR! node_modules/vue
npm ERR!   vue@"^3.4.21" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer vue@"^2.5.17" from element-ui@2.15.14
npm ERR! node_modules/element-ui
npm ERR!   element-ui@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution. 
npm ERR!
npm ERR!
npm ERR! For a full report see:
npm ERR! C:\Users\26557\AppData\Local\npm-cache\_logs\2024-03-20T02_38_58_639Z-eresolve-report.txt
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\26557\AppData\Local\npm-cache\_logs\2024-03-20T02_38_58_639Z-debug-0.log

奇奇怪怪的,一点都看不懂耶(),

解决办法

更换成以下代码

npm install element-plus --save

(据说是因为npm版本问题,使用elementplus就可以了,vue2不会报错)

谁偷了我的babelrc

官方文档说要在.babelrc中修改配置以达到按需引入,结果本小白看了一圈以后发现:嗯?!什么babelrc,在哪里呀?后来看见有个博客说改名成babel.config.js了,好吧,还是没找到╮(╯-╰)╭

老老实实按小兔鲜给的套路来了

解决办法:
打开终端,输入以下代码

npm install -D unplugin-vue-components unplugin-auto-import

然后在vite.config.js文件里添加如下代码:

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

并在plugins选项中的vue()后面添加如下配置:

    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({//1.配置elementPlus采用sass样式配色系统
      resolvers: [ElementPlusResolver()],
    }),

let's测试!

在App.vue文件中删除template标签中所有内容

更新成:

<template>
  <el-button type="primary">Primary</el-button>
</template>

完成啦~

  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,使用 Vite 和 TypeScript 结合 Element Plus 库可以按照以下步骤进行: 1. 安装 Vite: ```bash npm install -g vite ``` 2. 创建项目并进入项目目录: ```bash mkdir my-project cd my-project ``` 3. 初始化项目并安装 Element Plus 库: ```bash npm init -y npm install --save element-plus ``` 4. 创建 `index.html` 文件并引入 Element Plus 的样式文件和 JavaScript 文件: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>My Project</title> <link rel="stylesheet" href="https://unpkg.com/element-plus/lib/theme-chalk/index.css" /> </head> <body> <div id="app"></div> <script src="/src/main.ts"></script> </body> </html> ``` 5. 创建 `src/main.ts` 文件,导入 Element Plus 库的组件并创建 Vue 实例: ```typescript import { createApp } from 'vue'; import { ElButton, ElInput } from 'element-plus'; import App from './App.vue'; const app = createApp(App); app.component('el-button', ElButton); app.component('el-input', ElInput); app.mount('#app'); ``` 6. 创建 `src/App.vue` 文件,编写 Vue 模板: ```html <template> <div> <el-input v-model="inputValue" placeholder="请输入内容"></el-input> <el-button @click="handleClick">点击</el-button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ data() { return { inputValue: '', }; }, methods: { handleClick() { console.log(`输入的内容是:${this.inputValue}`); }, }, }); </script> ``` 7. 运行项目: ```bash vite ``` 8. 在浏览器中打开 `localhost:3000` 查看效果。 注意:以上代码只是一个简单的示例,实际开发中要根据具体求进行相应的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值