当前位置:   article > 正文

Element UI 组件全局设置默认配置_element 全局配置组件

element 全局配置组件

Element UI 组件库有时候想全局设置一些默认配置,避免重复代码。

以vue项目为例,把以下js引入到main.js即可

  1. import Vue from 'vue'
  2. import {
  3. Pagination,
  4. Dialog,
  5. Autocomplete,
  6. Dropdown,
  7. DropdownMenu,
  8. DropdownItem,
  9. Menu,
  10. Submenu,
  11. MenuItem,
  12. Input,
  13. InputNumber,
  14. Radio,
  15. RadioGroup,
  16. RadioButton,
  17. Checkbox,
  18. CheckboxButton,
  19. CheckboxGroup,
  20. Switch,
  21. Select,
  22. Option,
  23. OptionGroup,
  24. Button,
  25. ButtonGroup,
  26. Table,
  27. TableColumn,
  28. TimeSelect,
  29. Popover,
  30. Tooltip,
  31. Form,
  32. FormItem,
  33. Tabs,
  34. TabPane,
  35. Tag,
  36. Tree,
  37. Icon,
  38. Row,
  39. Col,
  40. Upload,
  41. Progress,
  42. Steps,
  43. Step,
  44. Carousel,
  45. CarouselItem,
  46. Cascader,
  47. Container,
  48. Image,
  49. Backtop,
  50. Loading,
  51. MessageBox,
  52. Message,
  53. Notification,
  54. Drawer,
  55. Empty,
  56. Descriptions,
  57. DescriptionsItem,
  58. Divider,
  59. Scrollbar,
  60. Header,
  61. Main,
  62. Footer
  63. } from 'element-ui'
  64. import DatePicker from './date-picker'
  65. // ElementUI默认配置
  66. Dialog.props.closeOnClickModal.default = false
  67. Table.props.border = { type: Boolean, default: true }
  68. Table.props.highlightCurrentRow = { type: Boolean, default: true }
  69. Tooltip.props.openDelay = { type: Number, default: 700 }
  70. Vue.use(Pagination)
  71. Vue.use(Dialog)
  72. Vue.use(Autocomplete)
  73. Vue.use(Dropdown)
  74. Vue.use(DropdownMenu)
  75. Vue.use(DropdownItem)
  76. Vue.use(Menu)
  77. Vue.use(Submenu)
  78. Vue.use(MenuItem)
  79. Vue.use(Input)
  80. Vue.use(InputNumber)
  81. Vue.use(Radio)
  82. Vue.use(RadioGroup)
  83. Vue.use(RadioButton)
  84. Vue.use(Checkbox)
  85. Vue.use(CheckboxButton)
  86. Vue.use(CheckboxGroup)
  87. Vue.use(Switch)
  88. Vue.use(Select)
  89. Vue.use(Option)
  90. Vue.use(OptionGroup)
  91. Vue.use(Button)
  92. Vue.use(ButtonGroup)
  93. Vue.use(Table)
  94. Vue.use(TableColumn)
  95. Vue.use(DatePicker)
  96. Vue.use(TimeSelect)
  97. Vue.use(Popover)
  98. Vue.use(Tooltip)
  99. Vue.use(Form)
  100. Vue.use(FormItem)
  101. Vue.use(Tabs)
  102. Vue.use(TabPane)
  103. Vue.use(Tag)
  104. Vue.use(Tree)
  105. Vue.use(Icon)
  106. Vue.use(Row)
  107. Vue.use(Col)
  108. Vue.use(Upload)
  109. Vue.use(Progress)
  110. Vue.use(Steps)
  111. Vue.use(Step)
  112. Vue.use(Carousel)
  113. Vue.use(CarouselItem)
  114. Vue.use(Cascader)
  115. Vue.use(Container)
  116. Vue.use(Image)
  117. Vue.use(Backtop)
  118. Vue.use(Drawer)
  119. Vue.use(Empty)
  120. Vue.use(Descriptions)
  121. Vue.use(DescriptionsItem)
  122. Vue.use(Loading.directive)
  123. Vue.use(Divider)
  124. Vue.use(Scrollbar)
  125. Vue.use(Header)
  126. Vue.use(Main)
  127. Vue.use(Footer)
  128. Vue.prototype.$loading = Loading.service
  129. Vue.prototype.$msgbox = MessageBox
  130. Vue.prototype.$alert = MessageBox.alert
  131. Vue.prototype.$confirm = MessageBox.confirm
  132. Vue.prototype.$prompt = MessageBox.prompt
  133. Vue.prototype.$notify = Notification
  134. Vue.prototype.$message = Message

对于部分组件,会把配置default值暴露到props,就可以直接修改来设置,如下:

  1. // ElementUI默认配置
  2. Dialog.props.closeOnClickModal.default = false
  3. Table.props.border = { type: Boolean, default: true }
  4. Table.props.highlightCurrentRow = { type: Boolean, default: true }
  5. Tooltip.props.openDelay = { type: Number, default: 700 }

对于没有暴露出default属性的组件,如果要全局设置其默认值,可以通过extends继承原组件,再重新封装来设置,比如,可以通过以下方式设置日期组件的快捷按钮:

  1. import { DatePicker } from 'element-ui'
  2. const DatePickerPatched = {
  3. extends: DatePicker,
  4. props: {
  5. pickerOptions: { // 全局设置时间日期选择组件的快捷选项
  6. type: Object,
  7. default: () => {
  8. return {
  9. shortcuts: [{
  10. text: '最近一周',
  11. onClick(picker) {
  12. const end = new Date()
  13. const start = new Date()
  14. start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
  15. picker.$emit('pick', [start, end])
  16. }
  17. }, {
  18. text: '最近一个月',
  19. onClick(picker) {
  20. const end = new Date()
  21. const start = new Date()
  22. start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
  23. picker.$emit('pick', [start, end])
  24. }
  25. }, {
  26. text: '最近三个月',
  27. onClick(picker) {
  28. const end = new Date()
  29. const start = new Date()
  30. start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
  31. picker.$emit('pick', [start, end])
  32. }
  33. }, {
  34. text: '最近半年',
  35. onClick(picker) {
  36. const end = new Date()
  37. const start = new Date()
  38. start.setTime(start.getTime() - 3600 * 1000 * 24 * 183)
  39. picker.$emit('pick', [start, end])
  40. }
  41. }, {
  42. text: '最近一年',
  43. onClick(picker) {
  44. const end = new Date()
  45. const start = new Date()
  46. start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)
  47. picker.$emit('pick', [start, end])
  48. }
  49. }]
  50. }
  51. }
  52. }
  53. }
  54. }
  55. export default {
  56. install(Vue) {
  57. Vue.component(DatePicker.name, DatePickerPatched)
  58. }
  59. }

再引入这个封装后的date-picker替换原先的el-date-picker,即可全局默认设置快捷选项,如果某个场景的组件需要定制,重新设置即可覆盖此默认值。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/642983
推荐阅读
相关标签
  

闽ICP备14008679号