element-plus/website/docs/en-US/quickstart.md

5.1 KiB
Raw Blame History

Quick start

This section describes how to use ElementPlus in your project.

Using components

Full import

main.ts

// main.ts
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'

const app = createApp(App)

app.use(ElementPlus)
app.mount('#app')

Import on demand

ElementPlus provides out of box Tree Shaking functionalities based on ES Module.

App.vue

<template>
  <el-button>
    I am ElButton
  </el-button>
</template>
<script>
import { defineComponent } from 'vue'
import { ElButton } from 'element-plus'

export default defineComponent({
  name: 'app'
  components: {
    ElButton,
  },
})
</script>

Import stylesheets

It is strongly recommended that you import the bundled stylesheet file, even though this could increase the final output bundle size, but it requires no packaging plugins for bundling, you can use CDN to load your stylesheet which would be much more faster than hosting the file on your own server.

Import via JavaScript

import 'element-plus/dist/index.css'

Import via HTML head tag.

<!-- index.html -->
<head>
  <link rel="stylesheet" href="//unpkg.com/element-plus/dist/index.css">
</head>

If you also want the style to be imported on demand, you need to use co-responding plugins based on your bundler, read more: FAQs

Starter templates

Using vue-cli@4.5

We prepared a plugin Element Plus VueCLI plugin, for new version vue-cli, you can setup a project based on ElementPlus easily.

Using Starter Kit

We provide a general Project Template, also a Vite Template. For Laravel users we have a Laravel Template.

Global configuration

When registering ElementPlus, you can pass a global config object with size and zIndex to set the default size for form components, and zIndex for popup components, the default value for zIndex is 2000

Full import:

import { createApp } from 'vue'
import ElementPlus from 'element-plus';
import App from './App.vue';

const app = createApp(App)
app.use(ElementPlus, { size: 'small', zIndex: 3000 });

On demand:

import { createApp } from 'vue'
import { ElButton } from 'element-plus';
import App from './App.vue';

const app = createApp(App)
app.config.globalProperties.$ELEMENT = option
app.use(ElButton);

Using Nuxt.js

We can also use Nuxt.js

Let's get started

You can bootstrap your project from now on, for each components usage, please refer to individual component documentation.

FAQs

I'd like to import both components and style at the same time

You can achieve that based on the bundler you are currently using, with ElementPlus supported plugins.

Using vite

If you are using vite as your bundler, then you need to install vite-plugin-element-plus in order to get it work.

yarn add vite-plugin-element-plus -D
# or
npm install vite-plugin-element-plus -D

Then you need to add the code below into your vite.config.[t]js file.

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import VitePluginElementPlus from 'vite-plugin-element-plus'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    VitePluginElementPlus({
      // if you need to use the *.scss source file, you need to uncomment this comment
      // useSource: true
    }),
  ],
})

For all public API, you can refer to vite-plugin-element-plus

Using webpack

If you are using webpack as your bundler, then you need to install babel-plugin-import in order to get it work.

yarn add babel-plugin-import -D
# or
npm install babel-plugin-import -D

Then you need to add the code below into your babel.config.js file.

babel.config.js

module.exports = {
  plugins: [
    // ...others
    [
      "import",
      {
        libraryName: 'element-plus',
        customStyleName: (name) => {
          name = name.slice(3)
          // this imports the [name].css file into the project.
          return `element-plus/es/${name}/style/css`
          // if you wish to import the *.scss source file, please uncomment this
          // return `element-plus/es/${name}/style`;
        },
      },
    ],
  ],
};