>  기사  >  웹 프론트엔드  >  Element-plus in vue(코드 포함) 글로벌 도입 및 로컬 도입

Element-plus in vue(코드 포함) 글로벌 도입 및 로컬 도입

WBOY
WBOY앞으로
2022-08-10 17:21:056122검색

이 글은 vue3 통합 Element-plus의 글로벌 도입과 로컬 도입 방법에 관한 이슈를 주로 소개하는 vue에 대한 관련 지식을 함께 살펴보도록 하겠습니다.

Element-plus in vue(코드 포함) 글로벌 도입 및 로컬 도입

【관련 권장사항: javascript video tutorial, vue.js tutorial

첫 번째 다운로드 element-plus

npm install element-plus

1 첫 번째 방법은 전역 가져오기

를 사용하여 element-plus It을 도입하는 것입니다. 전역으로 가져오기 때문에 모든 구성 요소와 플러그인이 자동으로 등록됩니다.

장점: 시작이 빠릅니다.

단점: 패키지 크기가 늘어납니다.

main.ts 파일에서

import { createApp } from 'vue'
// 全局引入
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import store from './store'
 
const app = createApp(App)
app.use(router)
app.use(store)
app.use(ElementPlus)
app.mount('#app')

2 . 두 번째 유형 방법, 로컬 도입 사용

부분 도입은 개발 중에 특정 컴포넌트를 사용하여 특정 컴포넌트를 도입하는 것을 의미합니다.

<template>
  <div>
    <el-button>Default</el-button>
    <el-button type="primary">Primary</el-button>
    <el-button type="success">Success</el-button>
    <el-button type="info">Info</el-button>
    <el-button type="warning">Warning</el-button>
    <el-button type="danger">Danger</el-button>
    <el-button>中文</el-button>
  </div>
</template>
<script>
import { defineComponent } from &#39;vue&#39;
// 局部引入
import { ElButton } from &#39;element-plus&#39;
import &#39;element-plus/theme-chalk/el-button.css&#39;
import &#39;element-plus/theme-chalk/base.css&#39;
export default defineComponent({
  components: { ElButton },
  setup() {
    return {}
  }
})
</script>
 
<style></style>

하지만 이 방법은 사용할 때마다 해당 CSS 스타일을 컴포넌트에 수동으로 도입해야 합니다. 개발 중에 사용하기가 더 번거로울 것입니다

3. 필요에 따라 자동으로 element-plus를 도입합니다. 권장

설치가 필요합니다unplugin-vue-components 和 unplugin-auto-import이 두 플러그인

npm install -D unplugin-vue-components unplugin-auto-import

설치 후 vue.config.js 파일에서 구성하세요

// vue.config.js
const AutoImport = require(&#39;unplugin-auto-import/webpack&#39;)
const Components = require(&#39;unplugin-vue-components/webpack&#39;)
const { ElementPlusResolver } = require(&#39;unplugin-vue-components/resolvers&#39;)
module.exports = {
  outputDir: &#39;./build&#39;,
  // 和webpapck属性完全一致,最后会进行合并
  configureWebpack: {
    resolve: {
      alias: {
        components: &#39;@/components&#39;
      }
    },
    //配置webpack自动按需引入element-plus,
      plugins: [
        AutoImport({
          resolvers: [ElementPlusResolver()]
        }),
        Components({
          resolvers: [ElementPlusResolver()]
        })
      ]
  }
}

온디맨드 자동 도입 구성 후 참조나 등록 없이 컴포넌트에서 직접 사용할 수 있습니다. 요청 시 자동으로 Element-plus 컴포넌트로 이동되어 직접 사용됩니다.

<template>
  <div>
    <el-button>Default</el-button>
    <el-button type="primary">Primary</el-button>
    <el-button type="success">Success</el-button>
    <el-button type="info">Info</el-button>
    <el-button type="warning">Warning</el-button>
    <el-button type="danger">Danger</el-button>
    <el-button>中文</el-button>
  </div>
</template>
<script>
import { defineComponent } from &#39;vue&#39;
export default defineComponent({
  setup() {
    return {}
  }
})
</script>
 
<style></style>

효과:

확장:

방법 1. 전역 참조(모든 구성 요소가 통합됨)

장점: 통합이 상대적으로 간단함

단점: 모든 구성 요소와 스타일이 패키지화되어 크기가 커집니다. Volume

사용법: npm install element-plus --save

main.ts에서 js 및 css 파일을 참조하세요

About.vue 페이지를 예로 들어보세요. 구성 요소는 기본적으로 전역적으로 등록되어 있습니다. 페이지에 다시 등록할 필요가 없습니다.

방법 2: 부분 인용(요청 시 인용)

장점: 패키지가 작아진다

단점: 인용이 조금 더 번거롭다

사용법 1: About.vue 페이지를 예로 들어, 페이지의 js 파일을 인용하고, 컴포넌트를 로컬에 등록하세요. 스타일은 여전히 ​​전역 참조입니다. 공식 권장 사항

【 관련 권장 사항: javascript 비디오 튜토리얼, vue.js 튜토리얼

위 내용은 Element-plus in vue(코드 포함) 글로벌 도입 및 로컬 도입의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 jb51.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제