亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue2中引入使用ElementUI的教程詳解

 更新時間:2024年03月18日 09:10:11   作者:Saga?Two  
這篇文章主要為大家詳細介紹了Vue2中引入使用ElementUI教程的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的可以參考下

1 安裝

推薦使用 npm 的方式安裝,它能更好地和 webpack打包工具配合使用。(本項目使用安裝方式)

npm i element-ui -S

也可以使用其他的包管理起進行安裝:

# Yarn
$ yarn add element-ui

# pnpm
$ pnpm install element-ui

2 引入

ElementUI分為全局引入和按需引入兩種方式,一般在工程項目中,如果使用全局引入,則項目初始化時會導致不必要的資源加載,為提升項目性能,建議進行按需引入。以下我們對兩種引入方式進行介紹。

2.1 全局引入

2.1.1 引入

在 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 的引入。

2.1.2 使用

引入完成之后就可以使用組件了,如下示例為使用container組件和button組件:

效果如下:

代碼如下:

<template>
  <el-container>
    <el-header>Header</el-header>
    <el-container>
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-main>
          Main
          <el-button type="primary">按鈕</el-button>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>
<script>
export default {};
</script>
<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

2.2 按需引入

可以使用babel-plugin-component這個Babel插件。這樣,你可以只引入你實際使用的組件和它們的樣式,從而減小項目體積和構建時間。

2.2.1 引入

首先,安裝 babel-plugin-component:

npm install babel-plugin-component -D

然后,將 .babelrc 或者babel.config.js文件修改為:

{
  "presets": [["es2015", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

2.2.2 使用

若想實現上圖效果,按需引入時需要將使用的所有組件都引入進來,代碼如下:

<template>
  <el-container>
    <el-header>Header</el-header>
    <el-container>
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-main>
          Main
          <el-button type="primary">按鈕</el-button>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>
<script>
import Vue from "vue";
import { Button, Container, Header, Aside, Main, Footer } from "element-ui";
Vue.use(Button);
Vue.use(Container);
Vue.use(Aside);
Vue.use(Main);
Vue.use(Footer);
Vue.use(Header);

export default {};
</script>
<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

按需引入組件,組件全部名稱詳見官網;

3 總結

通常情況下,若是對性能沒有要求時,可以使用全局導入方式引入所有組件,若對頁面加載性能有要求,則最好使用按需加載方式引入組件,以防多余的資源加載增加頁面初始化耗時。

到此這篇關于Vue2中引入使用ElementUI的教程詳解的文章就介紹到這了,更多相關Vue2 ElementUI內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 用Vue.extend構建消息提示組件的方法實例

    用Vue.extend構建消息提示組件的方法實例

    本篇文章主要介紹了用Vue.extend構建消息提示組件的方法實例,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • Vant的安裝和配合引入Vue.js項目里的方法步驟

    Vant的安裝和配合引入Vue.js項目里的方法步驟

    這篇文章主要介紹了Vant的安裝和配合引入Vue.js項目里的方法步驟,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 基于Vue實現timepicker

    基于Vue實現timepicker

    這篇文章主要為大家詳細介紹了基于Vue實現timepicker效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 從0到1構建vueSSR項目之node以及vue-cli3的配置

    從0到1構建vueSSR項目之node以及vue-cli3的配置

    這篇文章主要介紹了從0到1構建vueSSR項目之node以及vue-cli3的配置,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • vue全局自定義指令-元素拖拽的實現代碼

    vue全局自定義指令-元素拖拽的實現代碼

    這篇文章主要介紹了面板拖拽之vue自定義指令,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • 在Vue項目中使用snapshot測試的具體使用

    在Vue項目中使用snapshot測試的具體使用

    這篇文章主要介紹了在Vue項目中使用snapshot測試的具體使用,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • 使用 vue.js 構建大型單頁應用

    使用 vue.js 構建大型單頁應用

    本文給大家詳細介紹了如何使用使用 vue.js腳手架工具vue-cli構建大型單頁應用的方法,非常的實用,有需要的小伙伴可以參考下
    2018-02-02
  • vue+element實現動態(tài)換膚的示例代碼

    vue+element實現動態(tài)換膚的示例代碼

    本文主要介紹了vue+element實現動態(tài)換膚的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 淺談Vue響應式(數組變異方法)

    淺談Vue響應式(數組變異方法)

    這篇文章主要介紹了淺談Vue響應式(數組變異方法),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • vue里面v-bind和Props 利用props綁定動態(tài)數據的方法

    vue里面v-bind和Props 利用props綁定動態(tài)數據的方法

    今天小編就為大家分享一篇vue里面v-bind和Props 利用props綁定動態(tài)數據的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評論