• 基于el-ement 封装v-model


    父组件
    @change="fetch">
    子组件
    HandleList.vue

    1. <template>
    2. <div class="handle-box">
    3. <el-button type="primary" size="mini" @click="addFun">
    4. <svg-icon icon-class="add" />&nbsp;&nbsp;<span>新建</span>
    5. </el-button>
    6. <slot />
    7. <div class="search">
    8. <el-input v-model.lazy="inValue" size="small" placeholder="请输入内容" width="200" @change="change" />
    9. <div class="btn">
    10. <svg-icon icon-class="search1" class="search1" @click.stop="change" />
    11. </div>
    12. </div>
    13. </div>
    14. </template>
    15. <script>
    16. export default {
    17. name: 'HandleList',
    18. model: {
    19. prop: 'modelValue',
    20. event: 'returnModel'
    21. },
    22. props: {
    23. // 新建
    24. add: {
    25. type: Boolean,
    26. default: true
    27. },
    28. // 输入框
    29. search: {
    30. type: Boolean,
    31. default: true
    32. },
    33. modelValue: String
    34. },
    35. data() {
    36. return {
    37. inputVal: this.value
    38. }
    39. },
    40. computed: {
    41. inValue: {
    42. get: function() {
    43. // 获取传入的v-model
    44. return this.modelValue
    45. },
    46. set: function(newValue) {
    47. // 修改时同时修改v-model
    48. this.$emit('returnModel', newValue)
    49. return newValue
    50. }
    51. }
    52. },
    53. watch: {
    54. },
    55. methods: {
    56. change() {
    57. console.log('到了这里')
    58. this.$emit('change')
    59. },
    60. addFun() {
    61. this.$emit('add')
    62. }
    63. }
    64. }
    65. </script>
    66. <style lang="scss" scoped>
    67. .modal {
    68. .slot-title {
    69. width: 100%;
    70. height: 28px;
    71. font-family: PingFangSC-Semibold;
    72. font-size: 20px;
    73. color: #2088ff;
    74. letter-spacing: 0;
    75. font-weight: 600;
    76. }
    77. /deep/ .el-dialog__header {
    78. border-bottom: 1px solid $lightgrey;
    79. }
    80. /deep/ .el-dialog__body {
    81. padding: 20px 20px;
    82. }
    83. /deep/ .el-dialog__footer {
    84. border-top: 1px solid $lightgrey;
    85. padding: 15px 20px;
    86. .btn {
    87. width: 104px;
    88. height: 32px;
    89. padding: 0px;
    90. border-radius: 2px;
    91. }
    92. }
    93. }
    94. </style>

      

    • model父组件v-mode传入的数据
      • prop :对应prop的modelValue
      • event:修改v-model的事件

    props: {

        modelValue: String
    }

    • 不能直接操作modelValue.      数据向下,事件向上原则

        computed: {
          inValue: {
            get: function() {
              // 获取传入的v-model
              return this.modelValue
            },
            set: function(newValue) {
              // 修改时同时修改v-model
              this.$emit('returnModel', newValue)
              return newValue
            }
          }

        },

    用computed 的计算一个新的参数 给el-input 绑定

    如果要实现
    v-model.lazy 效果
    需要在子组件
      <el-input v-model.lazy="inValue" size="small" placeholder="请输入内容" width="200" @change="change" />

        change() {
          console.log('到了这里')
          this.$emit('change')
        },
    父组件
    @change="fetch">

    model父组件v-mode传入的数据
    prop :对应prop的modelValue
    event:修改v-model的事件
    为什么不直接 v-model 绑定 modelValue
    因为el-input修改同时会修改modelValue然后vue的prop数据是不允许被子组件修改的
    因此需要对应的event修改
    为了显示数据inValue单向绑定,set调用returnModel,get获取v-model值

    扩展点:
    v-model语法糖   是:value  @input 实现
    这个可能是个面试题
    注:我只是复制了部分代码,部分props参数逻辑没写完,抛砖引玉
     

  • 相关阅读:
    SpringClouldAlibaba 之 初识 Sentinel
    Apache服务器优化
    Python如何实现原型设计模式?什么是原型设计模式?Python 原型设计模式示例代码
    飞书公式总结
    vue部署,chunk文件有部分404,解决方案
    游戏工程管理
    docker镜像的创建
    uniapp 基础
    这七个Github仓库,够学一辈子!
    uniapp 在 onLoad 事件中 this.$refs 娶不到的问题
  • 原文地址:https://blog.csdn.net/weixin_41127362/article/details/126698422