Skip to content

7. Vue UI组件库

如果UI组件库提供的样式或者交互我们不满意,是可以写一些css和js去针对性地修改的!

不需要进行定制化、独特化的前端项目,更适合用UI组件库

如果想打造一个自己专属的,那么不适合用UI组件库

7.1 常用UI组件库

7.1.1 移动端常用UI组件库

  1. Vant
  2. Cube UI
  3. Mint UI

7.1.2. PC端常用UI组件库

基于Vue的:

  1. Element UI
  2. IView UI

7.2. element-ui基本使用

  1. 安装 element-ui:

    bash
    npm i element-ui -S
  2. src/main.js:

    js
    import Vue from 'vue'
    import App from './App.vue'
    
    //引入ElementUI组件库
    import ElementUI from 'element-ui';
    //引入ElementUI全部样式
    import 'element-ui/lib/theme-chalk/index.css';
    
    Vue.config.productionTip = false
    
    //使用ElementUI,但是这里引入了100多个组件,全部的样式,体积太大,所以会造成比较慢
    Vue.use(ElementUI)
    
    new Vue({
        el:"#app",
        render: h => h(App),
    })
  3. src/App.vue:

    vue
    <template>
    	<div>
    		<br>
    		<el-row>
    			<el-button icon="el-icon-search" circle></el-button>
    			<el-button type="primary" icon="el-icon-edit" circle></el-button>
    			<el-button type="success" icon="el-icon-check" circle></el-button>
    			<el-button type="info" icon="el-icon-message" circle></el-button>
    			<el-button type="warning" icon="el-icon-star-off" circle></el-button>
    			<el-button type="danger" icon="el-icon-delete" circle></el-button>
    		</el-row>
    	</div>
    </template>
    
    <script>
    	export default {
    		name:'App',
    	}
    </script>

效果:

img

7.3. element-ui按需引入

  1. 安装 babel-plugin-component 插件:
bash
npm install babel-plugin-component -D # -D是开发依赖的意思
  1. 修改 babel.config.js:用于支持babel-plugin-component插件

注:如果是vue-cli3创建的项目,没有babel.config.js,可以在.babelrc文件中书写,效果是一样的!!!

js
module.exports = {
  // 这里是本来就有的!
  presets: [ //预设包
    '@vue/cli-plugin-babel/preset', //用于解析js
    ["@babel/preset-env", { "modules": false }]
  ],
  // 只有下面这里是新增的!
  plugins: [ //插件
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}
  1. src/main.js:

引入时的技巧:用的标签叫el-xxx,那么就把el-去掉,然后首字母大写,就是我们需要引入的组件名了

js
import Vue from 'vue'
import App from './App.vue'

//完整引入 ——> 体积太大,不推荐
//引入element-ui组件库
// import ElementUI from 'element-ui';
//引入element全部样式
// import 'element-ui/lib/theme-chalk/index.css';
//使用element-ui
//Vue.use(ElementUI)

//按需引入 ——> 推荐
import { Button, Input, Row, DatePicker } from 'element-ui'; //这里不仅引入了组件,还自动把组件的样式引入了,所以样式我们不用关心

Vue.config.productionTip = false

Vue.use(Button); //el-button
Vue.use(Input); //el-input
Vue.use(Row); //el-row
Vue.use(DatePicker); //el-date-picker

/* 上面的Vue.use(xxx)或写为:
  Vue.component(Button.name, Button); //Vue.component()用于注册全局组件!
  Vue.component(Input.name, Input); //Input.name默认就为el-input,尽量不要自己取名,如果自己取名了,在使用标签时就要使用自己命的名字!
  Vue.component(Row.name, Row); //Row.name默认就为el-row
  Vue.component('atguigu', DatePicker);
 */

new Vue({
    el:"#app",
    render: h => h(App),
})