Element Plus インストールガイド

このガイドでは、さまざまなプロジェクトでElement Plusをインストールして使用する方法を詳しく説明します。

環境要件

Element Plusの動作環境以下のには要件があります:

  • Vue 3.0.0+
  • Node.js 14.0.0+
  • 最新のブラウザ(Chrome、Firefox、Safari、Edge)

インストール方法

パッケージマネージャーの使用

npm、yarn、またはpnpmでのインストールをお勧めします:

# npm
npm install element-plus

# yarn
yarn add element-plus

# pnpm
pnpm add element-plus

完全インポート

バンドルサイズを気にしない場合は、完全インポートの方が便利です。

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')

Volarサポート

Volarを使用する場合は、tsconfig.jsonのcompilerOptions.typesでグローバルコンポーネントタイプを指定してください:

{
  "compilerOptions": {
    "types": ["element-plus/global"]
  }
}

オンデマンドインポート

オンデマンドインポートを使用すると、プロジェクトのサイズを大幅に削減できます。追加のプラグインを使用して、必要なコンポーネントをインポートする必要があります。

自動インポート(推奨)

まず、プラグインをインストールします:

npm install -D unplugin-vue-components unplugin-auto-import
# または
yarn add -D unplugin-vue-components unplugin-auto-import
# または
pnpm add -D unplugin-vue-components unplugin-auto-import

Vite設定

vite.config.tsで構成します:

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

export default defineConfig({
  plugins: [
    AutoImport({ resolvers: [ElementPlusResolver()] }),
    Components({ resolvers: [ElementPlusResolver()] }),
  ],
})

Webpack設定

webpack.config.jsで構成します:

const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')

module.exports = {
  plugins: [
    AutoImport({ resolvers: [ElementPlusResolver()] }),
    Components({ resolvers: [ElementPlusResolver()] }),
  ],
}

Nuxtの使用

Nuxtユーザーは、@element-plus/nuxtをインストールするだけです:

npm install -D @element-plus/nuxt

次に、構成をnuxt.config.tsに記述します:

export default defineNuxtConfig({
  modules: ['@element-plus/nuxt'],
})

手動インポート

Element Plusは、ESモジュールに基づいた箱から出してすぐに使えるTree Shaking機能を提供します。

スタイルをインポートするにはunplugin-element-plusをインストールする必要があります:

npm install -D unplugin-element-plus

コンポーネントで manualmenteインポートします:

<template>
  <el-button>ボタン</el-button>
</template>

<script>
import { ElButton } from 'element-plus'

export default {
  components: { ElButton },
}
</script>

Vite設定:

import { defineConfig } from 'vite'
import ElementPlus from 'unplugin-element-plus/vite'

export default defineConfig({
  plugins: [ElementPlus()],
})

クイックスタートテンプレート

Element Plusは公式プロジェクトテンプレートを提供します:

グローバル設定

Element Plusを登録すると、グローバル設定オブジェクトを渡すことができます。

完全インポート設定

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

設定オプションの説明:

  • size:フォームコンポーネントのデフォルトサイズを設定(large、default、small)
  • zIndex:ポップアップコンポーネントのz-indexを設定、デフォルト値は2000

オンデマンドインポート設定

el-config-providerコンポーネントを使用して設定します:

<template>
  <el-config-provider :size="size" :z-index="zIndex">
    <app />
  </el-config-provider>
</template>

<script>
import { defineComponent } from 'vue'
import { ElConfigProvider } from 'element-plus'

export default defineComponent({
  components: { ElConfigProvider },
  setup() {
    return { zIndex: 3000, size: 'small' }
  },
})
</script>

詳細については、Element Plus公式ウェブサイトをご覧ください。