美文网首页
Vue-cli ElementUI 的使用

Vue-cli ElementUI 的使用

作者: 饥人谷1904_陈俊锋 | 来源:发表于2019-10-18 23:43 被阅读0次

官网:链接

安装:推荐使用 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>

可以使用 Element 中的默认样式,也可以通过修改底层代码或者CSS覆盖样式的方式来修改 Element 组件的样式。

在 js 文件中绑定事件:

export default {
  data() {
    
  },

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

效果如下:


组件测试.PNG

相关文章

网友评论

      本文标题:Vue-cli ElementUI 的使用

      本文链接:https://www.haomeiwen.com/subject/xzxgmctx.html