通过Vscode 简单创建一个vue3+element的项目

首先确保安装的nodejs是18版本以上

确保你安装了最新版本的 Node.js,并且你的当前工作目录正是打算创建项目的目录。在命令行中运行以下命令

VSCode打开终端

输入构建项目命令,个人推荐如果有cnpm使用cnpm

Go 复制代码
npm create vue@latest
cnpm create vue@latest

创建成功之后

引入element依赖包

java 复制代码
cnpm i element-ui -S
完整引入

在 main.js 中写入以下内容:

复制代码
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';

Vue.use(ElementUI);

new Vue({
  el: '#app',
  render: h => h(App)
});

以上代码便完成了 Element 的引入。需要注意的是,样式文件需要单独引入。

按需引入

借助 babel-plugin-component,我们可以只引入需要的组件,以达到减小项目体积的目的。

首先,安装 babel-plugin-component:

复制代码
npm install babel-plugin-component -D

然后,将 .babelrc 修改为:

复制代码
{
  "presets": [["es2015", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

接下来,如果你只希望引入部分组件,比如 Button 和 Select,那么需要在 main.js 中写入以下内容:

复制代码
import Vue from 'vue';
import { Button, Select } from 'element-ui';
import App from './App.vue';

Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
/* 或写为
 * Vue.use(Button)
 * Vue.use(Select)
 */

new Vue({
  el: '#app',
  render: h => h(App)
});
相关推荐
tan180°4 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
浪裡遊5 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
whale fall6 小时前
npm install安装的node_modules是什么
前端·npm·node.js
会飞的鱼先生6 小时前
Node.js-http模块
网络协议·http·node.js
万千思绪8 小时前
【PyCharm 2025.1.2配置debug】
ide·python·pycharm
用户3521802454759 小时前
MCP极简入门:node+idea运行简单的MCP服务和MCP客户端
node.js·ai编程
我在看世界10 小时前
家里vscode连公司内网vscede
vscode·ssh
不想迷路的小男孩12 小时前
Android Studio 中Palette跟Component Tree面板消失怎么恢复正常
android·ide·android studio
AlickLbc13 小时前
在phpstudy环境下配置搭建XDEBUG配合PHPSTORM的调试环境
ide·phpstorm
悠悠小茉莉13 小时前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio