vue中实现输入字数限制

实现思路

使用计算属性监听输入文本的字数,如果超出字数,则使用字符串截取,截取限制长度

代码

 <div class="description">
        <div class="desc-title">{{descTitle}}div>
        <div class="desc-content">
          <textarea
            class="desc-textarea"
            name="desc"
            cols="30"
            rows="10"
            :placeholder="inputDesc"
            v-model="descVal"
          >textarea>
          <div class="desc-textarea-counter">
            <span>{{descNum}}span>
            /140
          div>
        div>
      div>
  computed: {
    descNum() {
      if(this.descVal.length>=140){
        this.descVal=this.descVal.substr(0,140);
        return 140;
      }
      return this.descVal.length == 0?0:this.descVal.length;
    }
  },

你可能感兴趣的:(功能)