【问题标题】:Limit string length in vue material chips限制Vue材料芯片中的字符串长度
【发布时间】:2020-11-11 05:31:06
【问题描述】:

我正在使用Vue Material Chips component 收集一些字符串数组,并且我必须限制每个字符串的长度(比如说,30 个字符)。

他们有 md-limit 属性,它限制了我可以在我的数组中拥有的字符串数量,但不限制我可以在每个字符串上拥有的字符数。

我相信在将每个字符串添加到数组之前,我必须构建一个自定义验证(我尝试使用 v-validate,但它似乎对芯片组件没有任何影响)。

如果有人建议我如何实现它,将不胜感激!天呐

【问题讨论】:

    标签: javascript vue.js vue-material


    【解决方案1】:

    如果我做对了

    1. 您的芯片内部有一个阵列(例如: this.fruits = ['Banana','Apple','Pineapple','SomeOtherFruitWithMoreThan30Chars'])
    2. 当您按 Enter 键时,v-model 变量会更新并将您的输入推送到该数组中

    我会做什么:

    1. 做一个深表(也许正常工作正常)
    watch: {
      fruits:{
        // newVal is the new value of the array
        handler: function(newVal) {
    
          //checks if the last element of the array has more than 30 chars
          if(newVal[(newVal.length - 1)].length > 30){
            //remove the last element of the array
            this.fruits.pop()
          }
        },
        deep: true
      }
    }
    
    1. 尝试使用组件的格式化函数作为验证检查 md-format 如果超过 30 个字符则返回 null

    【讨论】:

    • tnx,它已经帮助了我很多!我可能需要向芯片字段发送一条错误消息,但对于我在这里暴露的问题,你的答案很好=]
    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多