Vue-cli ElementUI 的使用

ElementUI 官网链接

安装:推荐使用 npm

npm i element-ui -s

ElementUI 为新版的 vue-cli 准备了相应的 Element插件,可以快速搭建一个基于 Element 的项目。

引入 Element

可以引入整个 Element,或者根据需要引入部分组件。

  • 完整引入

在 vue-blog main.js 中写入

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

这里需要注意的是 样式文件 需要单独引入。

  • 按需引入

借助 babel-plugin-component 可以只引入需要的组件,以达到减小项目体积的目的。

首先应该安装 babel-plugin-component:

npm install babel-plugin-component -D

然后将 .babelrc 修改为:

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

接着把希望引入的部分组件写入 main.js:

import { Button, Select } from 'element-ui';   // 比如:Button 和 Select

Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
/* 或写为
 * Vue.use(Button)
 * Vue.use(Select)
 */

有一些常用的组件已经绑定到 Vue 的原型上,可以使用 this.$组件 来使用,或者写 组件.something

详细情况如下:

Vue.prototype.$loading = Loading.service;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;

使用组件

在文档中找到需要使用的组件的相应代码,加入到 vue 文件中:

<el-button @click="onClick1">Button 组件测试</el-button>

在 js 文件中绑定事件:

export default {
  data() {
    
  },

  methods: {
    onClick1() {
      this.$message('这是我使用的第一个 Element 组件')
    }
  }
}

效果如下:
Element 组件使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值