简体   繁体   English

如何使用 vuejs 计算属性评估文本区域的最大长度?

[英]How to evaluate max length of a text area using vuejs computed property?

I have a text area in a form that I am using to write the description of something.我有一个文本区域,我用它来写一些东西的描述。 But, the max char limit is 5 .但是,最大字符限制为5 I am trying to calculate the max length of my description using the computed property.我正在尝试使用计算属性计算我的描述的最大长度。 But, somehow the computed property is not firing when the length of the description crosses the limit of 5 chars.但是,当描述的长度超过 5 个字符的限制时,计算属性不会以某种方式触发。 Following is my simple code.以下是我的简单代码。

  props: {
    infoData: {
      type: Object,
      default: () => {
        return {
          category: "",
          side_categories: "",
          description: "",
          commentValidationState: null
        };
      }
    },
  },
  computed: {
    descriptionValidation(){
      if(this.infoData.description?.length > 5){
        alert("Max length exceeded!");
      }
    }
  }

It is noted that I am using the prop directly in the computed property.请注意,我直接在计算属性中使用道具。

My HTML:我的 HTML:

  <b-form-group
          class="col-md-12"
          label="Beschreibung"
          label-for="description"
          invalid-feedback="maximal 750 Zeichen"
          :state="infoData.commentValidationState"
      >
        <b-textarea
            class="form-control"
            name="description"
            id="description"
            v-model="infoData.description"
        />
      </b-form-group>

Computed properties must return the result of some computation.计算属性必须返回一些计算的结果。 Here, a watcher would be more appropriate.在这里,观察者会更合适。 In this case, the value to watch would be the length of this.infoData.description .在这种情况下,要观察的值是this.infoData.description的长度。 Consequently, I would first use a computed property to get the length of this.infoData.description and then use a watcher to monitor its value.因此,我将首先使用计算属性来获取this.infoData.description的长度,然后使用观察程序来监视其值。

Here is my implementation:这是我的实现:

<template>
  <div>
      <!--- Component HTML -->
   </div>
</template>

<script>
export default {
  props: {
    infoData: {
      type: Object,
      default: () => {
        return {
          category: "",
          side_categories: "",
          description: "",
          commentValidationState: null
        };
      }
    },
  },
  watch: {
    descriptionLength(new_value){
      if(new_value> 5){
        alert("Max length exceeded!");
      }
    }
  },
  computed: {
    descriptionLength(){
      return this.infoData.description?.length
    }
  }
}
</script>

And here is its parent:这是它的父母:

<template>
  <div>
    <textarea v-model="infoData.description" />
    <MyComponent :infoData="infoData" />
  </div>
</template>

<script>
import MyComponent from '@/components/MyComponent.vue'
export default {
  components: {
    MyComponent,
  },
  data() {
    return {
      infoData: {
          category: "",
          side_categories: "",
          description: "",
          commentValidationState: null
      }
    }
  },
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM