1、iView是一套基于Vue.js的开源UI组件库,主要服务于PC界面的中后台产品,比如某产品的运营平台、数据监控平台、管理平台等,从工程配置、到样式布局,甚至后面规划的业务套件,是一整套的解决方案,所以它可能不太适合一些to C 的产品,比如 QQ空间 这类的。Element 饿了么前端出品的一套 基于 Vue 2.0 的桌面端组件库,可用来构建中大型后台管理项目
2、搭建
#Element# Clone projectgit clone https://github.com/PanJiaChen/vue-admin-template.git# Install dependenciesnpm install# Serve with hot reload at localhost:9528npm run dev# Build for production with minificationnpm run build# Build for production and view the bundle analyzer reportnpm run build --report#VIEW UI#1、按需加载npm install babel-plugin-import --save-dev// .babelrc{"plugins": [["import", {"libraryName": "view-design","libraryDirectory": "src/components"}]]}然后这样按需引入组件,就可以减小体积了:import { Button, Table } from 'view-design';Vue.component('Button', Button);Vue.component('Table', Table);
3、设计风格
两者api 总体比较 ,iview 要比element 简洁许多。 饿了么更侧重于在template里直接去渲染模板
- 思想上 个人觉得iview偏向react, element 更vue
- 表单校验 两者都使用同一款插件 async-validator 校验方式一样
Ant Design 在 标准、视觉 上更胜一筹,示例少,成熟度不够
element国内最早,生态好
view ui 由公司维护,精致,偏向react设计思想,付费
4、兼容性
view ui to B 只能兼容到IE9
断言
你不必为了可测性在组件中做任何特殊的操作,导出原始设置就可以了:
<template><span>{{ message }}</span></template><script>export default {data () {return {message: 'hello!'}},created () {this.message = 'bye!'}}</script>
然后随着 Vue Test Utils 导入组件,你可以使用许多常见的断言 (这里我们使用的是 Jest 风格的 expect 断言作为示例):
// 导入 Vue Test Utils 内的 `shallowMount` 和待测试的组件import { shallowMount } from '@vue/test-utils'import MyComponent from './MyComponent.vue'// 挂载这个组件const wrapper = shallowMount(MyComponent)// 这里是一些 Jest 的测试,你也可以使用你喜欢的任何断言库或测试describe('MyComponent', () => {// 检查原始组件选项it('has a created hook', () => {expect(typeof MyComponent.created).toBe('function')})// 评估原始组件选项中的函数的结果it('sets the correct default data', () => {expect(typeof MyComponent.data).toBe('function')const defaultData = MyComponent.data()expect(defaultData.message).toBe('hello!')})// 检查 mount 中的组件实例it('correctly sets the message when created', () => {expect(wrapper.vm.$data.message).toBe('bye!')})// 创建一个实例并检查渲染输出it('renders the correct message', () => {expect(wrapper.text()).toBe('bye!')})})
编写可被测试的组件
很多组件的渲染输出由它的 props 决定。事实上,如果一个组件的渲染输出完全取决于它的 props,那么它会让测试变得简单,就好像断言不同参数的纯函数的返回值。看下面这个例子:
<template><p>{{ msg }}</p></template><script>export default {props: ['msg']}</script>
你可以使用 Vue Test Utils 来在输入不同 prop 时为渲染输出下断言:
import { shallowMount } from '@vue/test-utils'import MyComponent from './MyComponent.vue'// 挂载元素并返回已渲染的组件的工具函数function getMountedComponent(Component, propsData) {return shallowMount(Component, {propsData})}describe('MyComponent', () => {it('renders correctly with different props', () => {expect(getMountedComponent(MyComponent, {msg: 'Hello'}).text()).toBe('Hello')expect(getMountedComponent(MyComponent, {msg: 'Bye'}).text()).toBe('Bye')})})
断言异步更新
由于 Vue 进行异步更新 DOM 的情况,一些依赖 DOM 更新结果的断言必须在 vm.$nextTick() resolve 之后进行:
// 在状态更新后检查生成的 HTMLit('updates the rendered message when wrapper.message updates', async () => {const wrapper = shallowMount(MyComponent)wrapper.setData({ message: 'foo' })// 在状态改变后和断言 DOM 更新前等待一刻await wrapper.vm.$nextTick()expect(wrapper.text()).toBe('foo')})
