经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
vue对组件进行二次封装
来源:cnblogs  作者:Pursue`  时间:2021/6/15 9:11:28  对本文有异议

vue对组件进行二次封装

经常遇到常用组件与设计图有微小区别的情况,但是自写组件功能又太单一(划掉 其实原因就是懒),这个时候对组件封装就很有用处
例如对 element 的 MessageBox 二次封装

组件有很多自定义内容

例如 MessageBox 可自定义配置不同内容。

  1. <template>
  2. <el-button type="text" @click="open">点击打开 Message Box</el-button>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. open() {
  8. const h = this.$createElement;
  9. this.$msgbox({
  10. title: '消息',
  11. message: h('p', null, [
  12. h('span', null, '内容可以是 '),
  13. h('i', { style: 'color: teal' }, 'VNode')
  14. ]),
  15. showCancelButton: true,
  16. confirmButtonText: '确定',
  17. cancelButtonText: '取消',
  18. beforeClose: (action, instance, done) => {
  19. if (action === 'confirm') {
  20. instance.confirmButtonLoading = true;
  21. instance.confirmButtonText = '执行中...';
  22. setTimeout(() => {
  23. done();
  24. setTimeout(() => {
  25. instance.confirmButtonLoading = false;
  26. }, 300);
  27. }, 3000);
  28. } else {
  29. done();
  30. }
  31. }
  32. }).then(action => {
  33. this.$message({
  34. type: 'info',
  35. message: 'action: ' + action
  36. });
  37. });
  38. }
  39. }
  40. }
  41. </script>

那么就可以根据组件的自定义内容去封装一个符合设计需求的组件

代码结构


index.js

  1. import { MessageBox } from 'element-ui'
  2. import './ConfirmBox.scss'
  3. export default function(
  4. title = '提示',
  5. message = '提示内容',
  6. icon = 'warning'
  7. ) {
  8. const h = this.$createElement
  9. return MessageBox({
  10. message: h('div', null, [
  11. h(
  12. 'div',
  13. {
  14. class: {
  15. 'confirm-box-header': true
  16. }
  17. },
  18. [
  19. h('svg-icon', {
  20. props: {
  21. 'icon-class': icon,
  22. 'class-name': 'confirm-box-icon'
  23. }
  24. }),
  25. h(
  26. 'span',
  27. {
  28. class: {
  29. 'confirm-box-title': true
  30. }
  31. },
  32. title
  33. )
  34. ]
  35. ),
  36. h(
  37. 'div',
  38. {
  39. class: {
  40. 'confirm-box-message': true
  41. }
  42. },
  43. message
  44. )
  45. ]),
  46. customClass: 'confirm-box',
  47. showCancelButton: true,
  48. confirmButtonText: '确定',
  49. cancelButtonText: '取消'
  50. })
  51. }

ConfirmBox.scss

  1. .confirm-box {
  2. padding-bottom: 24px;
  3. .el-message-box__content {
  4. padding: 36px 24px;
  5. .confirm-box-header {
  6. display: flex;
  7. flex-direction: row;
  8. justify-content: flex-start;
  9. align-items: center;
  10. }
  11. .confirm-box-icon {
  12. width: 16px;
  13. height: 16px;
  14. }
  15. .confirm-box-title {
  16. display: block;
  17. padding-left: 12px;
  18. font-size: 16px;
  19. font-weight: 500;
  20. color: $primary-font;
  21. line-height: 24px;
  22. }
  23. .confirm-box-message {
  24. padding: 12px 0 0 28px;
  25. font-size: 14px;
  26. font-weight: 400;
  27. color: $primary-font;
  28. line-height: 22px;
  29. }
  30. }
  31. }

使用方式

main.js 加以下两行

  1. import ConfirmBox from '@/components/ConfirmBox'
  2. Vue.prototype.$confirmBox = ConfirmBox

使用效果 看起来好像像那么回事(虽然不是自写组件,但是写起来快啊)

  1. this.$confirmBox(
  2. '我大标题',
  3. '我是内容'
  4. )
  5. .then(async () => {
  6. })
  7. .catch(() => {})

原文链接:http://www.cnblogs.com/WNpursue/p/14862771.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号