vue项目中按需引入element-ui的正确方法

1. 创建vue项目(版本@vue/cli 4.5.4):

vue create test

2. 安装 babel-plugin-component:

npm install babel-plugin-component -D

3. 安装element-ui:

npm install element-ui -S

4. 修改babel.config.js配置文件:

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
    ["@babel/preset-env", { "modules": false }]
  ],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

5. main.js中按需引入组件、全局注册组件:

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { Button, Input } from 'element-ui'; // 按需引入组件
// 注册全局组件
Vue.component(Button.name, Button);
Vue.component(Input.name, Input);
/* 或写为
 * Vue.use(Button)
 * Vue.use(Select)
 */

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

6. 使用element-ui组件:

<template>
  <div>
    <!-- <input type="text" placeholder="请输入任务"> -->
    <el-input v-model="input" placeholder="请输入内容"></el-input>
  </div>
</template>

<script>
  export default {
    name: 'MyHeader',
    data() {
      return {
        input: ''
      }
    }
  }
</script>

<style>

</style>

猜你喜欢

转载自blog.csdn.net/u010234868/article/details/123637003