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

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

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

VSCode打开终端

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

npm create vue@latest
cnpm create vue@latest

创建成功之后

引入element依赖包

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)
});

相关推荐

  1. vscode创建jest测试框架项目

    2024-01-12 13:00:01       24 阅读
  2. vscode 创建测试单个js文件项目

    2024-01-12 13:00:01       33 阅读
  3. vue创建项目

    2024-01-12 13:00:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 13:00:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 13:00:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 13:00:01       87 阅读
  4. Python语言-面向对象

    2024-01-12 13:00:01       96 阅读

热门阅读

  1. 【数据库学习】hive

    2024-01-12 13:00:01       52 阅读
  2. 深度学习中的正则化指的是什么?

    2024-01-12 13:00:01       61 阅读
  3. vue3 组件内判断是从哪个页面过来的

    2024-01-12 13:00:01       67 阅读
  4. golang常见算法题

    2024-01-12 13:00:01       59 阅读
  5. JPA的乐观锁实现并发执行SQL案例

    2024-01-12 13:00:01       49 阅读
  6. 测试工程师常用的ChatGPT通用提示词模板

    2024-01-12 13:00:01       54 阅读
  7. LeetCode [103] 二叉树的锯齿形层序遍历

    2024-01-12 13:00:01       62 阅读
  8. 安全加密算法

    2024-01-12 13:00:01       65 阅读
  9. 新版cnpmcore部署私有npm源全教程

    2024-01-12 13:00:01       47 阅读
  10. 浅谈MySQL之新增列

    2024-01-12 13:00:01       58 阅读