vue中文本域限制字数的方法

用watch方法,来限制字数

<template>
  <div class="box">
        <textarea  v-model="title" width="100%" ></textarea>
         <span>还可以输入{{this.titleMaxLength - this.title.length}}</span>
  </div>
</template>
<script>
export default {
  name: 'Box',
    data() {
        return {
            title: '',
            titleMaxLength: 60
        };
    },
    methods:{
    },
    watch: {
        title() {
            if (this.title.length > this.titleMaxLength) {
                this.title = String(this.title).slice(0, this.titleMaxLength);
            }
        }
    }
}
</script>
<style lang="less">
.box{
     100%;
    textarea{
         100%;
        height: 60px;
        border: none;
        outline: none;
        box-sizing: border-box;
    }
}
</style>

  

原文地址:https://www.cnblogs.com/yishifuping/p/10551866.html