2020-08-13 15:18:26 +08:00
## Inicio rápido
2020-10-16 11:14:34 +08:00
Esta sección te guía en el proceso de usar Element Plus con webpack en un proyecto.
2020-08-13 15:18:26 +08:00
2020-10-26 17:16:22 +08:00
### Use vue-cli@4.5
2020-08-13 15:18:26 +08:00
2020-10-26 17:16:22 +08:00
Proporcionamos un [plugin de Element ](https://github.com/element-plus/vue-cli-plugin-element-plus ) para vue-cli@4.5, que puede utilizar para construir rápidamente un proyecto basado en Element.
2020-08-13 15:18:26 +08:00
### Usa la plantilla de Kit de inicio
2020-10-27 18:26:07 +08:00
Proveemos una plantilla general [project template ](https://github.com/element-plus/element-plus-starter ), and also a Vite [template ](https://github.com/element-plus/element-plus-vite-starter ). Para los usuarios de Laravel, también tenemos [template ](https://github.com/element-plus/element-plus-in-laravel-starter ). Puedes descargarlas y agregarlas directamente también.
2020-08-13 15:18:26 +08:00
Si prefiere no utilizarlas, lee las siguientes secciones de este documento.
2020-10-16 11:14:34 +08:00
### Importando Element Plus
2020-08-13 15:18:26 +08:00
2020-10-16 11:14:34 +08:00
Puede importar Element Plus completamente o solamente importar lo que necesite. Comencemos importando todo.
2020-08-13 15:18:26 +08:00
#### Importando todo
En main.js:
```javascript
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
2020-10-16 11:14:34 +08:00
import ElementPlus from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
2020-08-13 15:18:26 +08:00
import App from './App.vue';
2020-10-16 11:14:34 +08:00
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
2020-08-13 15:18:26 +08:00
```
2020-10-16 11:14:34 +08:00
El código anterior importa Element Plus completamente. Nótese que el archivo CSS necesita ser incluido por separado.
2020-08-13 15:18:26 +08:00
#### En demanda
2021-03-27 19:18:36 +08:00
**Vue CLI**
2021-02-02 14:15:34 +08:00
Con la ayuda de [babel-plugin-import ](https://github.com/ant-design/babel-plugin-import ), podemos importar los componentes que necesitamos, haciendo nuestro proyecto más pequeño que de la otra manera.
2020-08-13 15:18:26 +08:00
2021-02-02 14:15:34 +08:00
Primero, instale babel-plugin-import:
2020-08-13 15:18:26 +08:00
```bash
2021-02-02 14:15:34 +08:00
npm install babel-plugin-import -D
2020-08-13 15:18:26 +08:00
```
2021-02-02 14:15:34 +08:00
Luego edite babel.config.js:
2020-08-13 15:18:26 +08:00
2021-03-27 19:18:36 +08:00
- import `.scss` style
:::warning
Please make sure that `sass` and `sass-loader` dependencies have been installed and import `element-plus/packages/theme-chalk/src/base.scss` in the entry file.
:::
```js
module.exports = {
plugins: [
[
"import",
{
libraryName: 'element-plus',
customStyleName: (name) => {
name = name.slice(3)
return `element-plus/packages/theme-chalk/src/${name}.scss` ;
},
},
],
],
};
```
- import `.css` style
2021-02-02 14:15:34 +08:00
```js
module.exports = {
plugins: [
2020-08-13 15:18:26 +08:00
[
2021-02-02 14:15:34 +08:00
"import",
2020-08-13 15:18:26 +08:00
{
2021-02-02 14:15:34 +08:00
libraryName: 'element-plus',
customStyleName: (name) => {
return `element-plus/lib/theme-chalk/${name}.css` ;
},
},
],
],
};
2020-08-13 15:18:26 +08:00
```
2021-03-27 19:18:36 +08:00
**Vite**
Firstly, install [vite-plugin-style-import ](https://github.com/anncwb/vite-plugin-style-import ):
```bash
$ npm install vite-plugin-style-import -D
```
or if you use `Yarn` as package manager
```bash
$ yarn add vite-plugin-style-import -D
```
Then edit vite.config.js:
- import `.scss` style
:::warning
Please make sure that the `sass` dependency have been installed and import `element-plus/packages/theme-chalk/src/base.scss` in the entry file.
:::
```js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport from 'vite-plugin-style-import'
export default defineConfig({
plugins: [
vue(),
styleImport({
libs: [{
libraryName: 'element-plus',
2021-04-06 11:28:18 +08:00
esModule: true,
ensureStyleFile: true,
2021-03-27 19:18:36 +08:00
resolveStyle: (name) => {
2021-04-06 11:28:18 +08:00
name = name.slice(3)
2021-03-27 19:18:36 +08:00
return `element-plus/packages/theme-chalk/src/${name}.scss` ;
},
resolveComponent: (name) => {
return `element-plus/lib/${name}` ;
},
}]
})
]
})
```
- import `.css` style
```js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport from 'vite-plugin-style-import'
export default defineConfig({
plugins: [
vue(),
styleImport({
libs: [
{
libraryName: 'element-plus',
2021-04-06 11:28:18 +08:00
esModule: true,
ensureStyleFile: true,
2021-03-27 19:18:36 +08:00
resolveStyle: (name) => {
return `element-plus/lib/theme-chalk/${name}.css` ;
},
resolveComponent: (name) => {
return `element-plus/lib/${name}` ;
},
}
]
})
]
})
```
2020-08-13 15:18:26 +08:00
Luego, si necesita Button y Select, edite main.js:
```javascript
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
import { ElButton, ElSelect } from 'element-plus';
2020-08-13 15:18:26 +08:00
import App from './App.vue';
2021-03-27 19:18:36 +08:00
// If you want to use the .scss style file, you need to import the base.scss file
// import 'element-plus/packages/theme-chalk/src/base.scss'
2020-08-13 15:18:26 +08:00
2020-11-23 16:28:06 +08:00
const app = createApp(App)
app.component(ElButton.name, ElButton);
app.component(ElSelect.name, ElSelect);
2020-08-13 15:18:26 +08:00
/* or
2020-11-23 16:28:06 +08:00
* app.use(ElButton)
* app.use(ElSelect)
2020-08-13 15:18:26 +08:00
*/
2020-11-23 16:28:06 +08:00
app.mount('#app')
2020-08-13 15:18:26 +08:00
```
2020-11-23 16:28:06 +08:00
Ejemplo completo (Referencia completa de componentes [reference ](https://github.com/element-plus/element-plus/tree/dev/packages ))
2020-08-13 15:18:26 +08:00
```javascript
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
import App from './App.vue';
2021-03-27 19:18:36 +08:00
// If you want to use the .scss style file, you need to import the base.scss file
// import 'element-plus/packages/theme-chalk/src/base.scss'
2020-08-13 15:18:26 +08:00
import {
2020-11-23 16:28:06 +08:00
ElAlert,
ElAside,
ElAutocomplete,
ElAvatar,
ElBacktop,
ElBadge,
ElBreadcrumb,
ElBreadcrumbItem,
ElButton,
ElButtonGroup,
ElCalendar,
ElCard,
ElCarousel,
ElCarouselItem,
ElCascader,
ElCascaderPanel,
ElCheckbox,
ElCheckboxButton,
ElCheckboxGroup,
ElCol,
ElCollapse,
ElCollapseItem,
ElCollapseTransition,
ElColorPicker,
ElContainer,
ElDatePicker,
ElDialog,
ElDivider,
ElDrawer,
ElDropdown,
ElDropdownItem,
ElDropdownMenu,
ElFooter,
ElForm,
ElFormItem,
ElHeader,
ElIcon,
ElImage,
ElInput,
ElInputNumber,
ElLink,
ElMain,
ElMenu,
ElMenuItem,
ElMenuItemGroup,
ElOption,
ElOptionGroup,
ElPageHeader,
ElPagination,
ElPopconfirm,
ElPopover,
ElPopper,
ElProgress,
ElRadio,
ElRadioButton,
ElRadioGroup,
ElRate,
ElRow,
2020-12-04 17:07:26 +08:00
ElScrollbar,
2020-11-23 16:28:06 +08:00
ElSelect,
ElSlider,
ElStep,
ElSteps,
ElSubmenu,
ElSwitch,
ElTabPane,
ElTable,
ElTableColumn,
2020-12-01 19:14:11 +08:00
ElTabs,
ElTag,
ElTimePicker,
ElTimeSelect,
2020-11-23 16:28:06 +08:00
ElTimeline,
ElTimelineItem,
ElTooltip,
ElTransfer,
ElTree,
ElUpload,
ElInfiniteScroll,
ElLoading,
ElMessage,
ElMessageBox,
ElNotification,
2020-10-16 11:14:34 +08:00
} from 'element-plus';
2020-08-13 15:18:26 +08:00
2020-11-23 16:28:06 +08:00
const components = [
ElAlert,
ElAside,
ElAutocomplete,
ElAvatar,
ElBacktop,
ElBadge,
ElBreadcrumb,
ElBreadcrumbItem,
ElButton,
ElButtonGroup,
ElCalendar,
ElCard,
ElCarousel,
ElCarouselItem,
ElCascader,
ElCascaderPanel,
ElCheckbox,
ElCheckboxButton,
ElCheckboxGroup,
ElCol,
ElCollapse,
ElCollapseItem,
ElCollapseTransition,
ElColorPicker,
ElContainer,
ElDatePicker,
ElDialog,
ElDivider,
ElDrawer,
ElDropdown,
ElDropdownItem,
ElDropdownMenu,
ElFooter,
ElForm,
ElFormItem,
ElHeader,
ElIcon,
ElImage,
ElInput,
ElInputNumber,
ElLink,
ElMain,
ElMenu,
ElMenuItem,
ElMenuItemGroup,
ElOption,
ElOptionGroup,
ElPageHeader,
ElPagination,
ElPopconfirm,
ElPopover,
ElPopper,
ElProgress,
ElRadio,
ElRadioButton,
ElRadioGroup,
ElRate,
ElRow,
2020-12-04 17:07:26 +08:00
ElScrollbar,
2020-11-23 16:28:06 +08:00
ElSelect,
ElSlider,
ElStep,
ElSteps,
ElSubmenu,
ElSwitch,
ElTabPane,
ElTable,
ElTableColumn,
ElTabs,
ElTag,
ElTimePicker,
ElTimeSelect,
ElTimeline,
ElTimelineItem,
ElTooltip,
ElTransfer,
ElTree,
ElUpload,
]
const plugins = [
ElInfiniteScroll,
ElLoading,
ElMessage,
ElMessageBox,
ElNotification,
]
const app = createApp(App)
components.forEach(component => {
app.component(component.name, component)
})
plugins.forEach(plugin => {
app.use(plugin)
})
2020-08-13 15:18:26 +08:00
```
### Configuración global
Cuando importa Element, puede definir un objeto global de configuración. Por ahora este elemento solo contiene dos propiedades: `size` , `zIndex` . `size` define el tamaño por defecto de todos los componentes.
La propiedad `zIndex` indica el z-index inicial (por defecto: 2000) para los modal:
2020-10-16 11:14:34 +08:00
Importando Element Plus completamente:
2020-08-13 15:18:26 +08:00
```js
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
2020-10-16 11:19:36 +08:00
import ElementPlus from 'element-plus';
2020-11-23 16:28:06 +08:00
import App from './App.vue';
const app = createApp(App)
app.use(ElementPlus, { size: 'small', zIndex: 3000 });
2020-08-13 15:18:26 +08:00
```
2020-10-16 11:14:34 +08:00
Importando Element Plus parcialmente:
2020-08-13 15:18:26 +08:00
```js
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
import { ElButton } from 'element-plus';
import App from './App.vue';
2021-03-27 19:18:36 +08:00
// If you want to use the .scss style file, you need to import the base.scss file
// import 'element-plus/packages/theme-chalk/src/base.scss'
2020-08-13 15:18:26 +08:00
2020-11-23 16:28:06 +08:00
const app = createApp(App)
app.config.globalProperties.$ELEMENT = option
app.use(ElButton);
2020-08-13 15:18:26 +08:00
```
Con la anterior configuración, el tamaño por defecto de todos los componentes que tienen el atributo `size` será `small` . El valor inicial de z-index para los modals se ha establecido a 3000.
### Empiece ya!
2020-10-16 11:14:34 +08:00
Ahora ha incorporado Vue y Element Plus a su proyecto y es el momento para comenzar a programar. Por favor, refiérase a la documentación de cada componente para aprender cómo usarlos.
2020-08-13 15:18:26 +08:00
### Use Nuxt.js
También podemos comenzar un proyecto usando [Nuxt.js ](nuxtjs.org ):
< div class = "glitch-embed-wrap" style = "height: 420px; width: 100%;" >
< iframe src = "https://glitch.com/embed/#!/embed/nuxt-with-element?path=nuxt.config.js&previewSize=0&attributionHidden=true" alt = "nuxt-with-element on glitch" style = "height: 100%; width: 100%; border: 0;" > < / iframe >
< / div >