1

I am following a vue tutorial. Below is the function to implement character limit on a text area ,

<form action="" class="create-twoot" @submit.prevent="createnewTwoot">
   <label for="new-twoot">New Twoot</label>
   <p>{{char_limit}}/180</p>
   <textarea name="" id="new-twoot" cols="30" rows="4" v-model="newTwootcontent"></textarea>
   <br/>
   <label for="newTwootType">Twoot Type</label>
   <select name="TwootType" id="newTwootType" v-model="twootType">
      <option :value="option.name" v-for="(option,index)  in twootTypes" :key="index">
         {{option.value}}
      </option>
   </select>
   <br/>
   <button>Tweet</button>
</form>

JS CODE

export default {
  name: 'Userprofile',
  newTwootcontent: '',
  twootType: 'instant',
  
    computed:{
    char_limit(){
      return this.newTwootcontent.length;
    },
    fullname(){
      return `${this.users.fname} ${this.users.lname}`;
    }
  },
}

Everything else is working fine but this char_limit is giving below error

(i did not post data , methods etc since these were working fine )

Cannot read property 'length' of undefined
    at Proxy.char_limit (Userprofile.vue?5045:102)

can someone advise me what's the issue with code

4
  • first check out the basics, Data and Methods: vuejs.org/v2/guide/instance.html#Data-and-Methods Commented Jan 29, 2021 at 14:19
  • @LawrenceCherone i have added methods and data and it was working find , pls let me know if i should post that code as well Commented Jan 29, 2021 at 14:22
  • post code as-is, else as-is you're defining what should be in data on the vue instance which is wrong, hence the error Commented Jan 29, 2021 at 14:24
  • @LawrenceCherone as per answer by Amaarrockz i defined newTwootcontent inside data and it solved the issue , Thanks Commented Jan 29, 2021 at 14:26

1 Answer 1

1

define the variables inside the data section like

export default {
  name: 'Userprofile',
  data() {
    return {
      newTwootcontent: '',
      twootType: 'instant',
    };
  }
    computed:{
    char_limit(){
      return this.newTwootcontent.length;
    },
    fullname(){
      return `${this.users.fname} ${this.users.lname}`;
    }
  },
}
Sign up to request clarification or add additional context in comments.

3 Comments

sure in few minutes it will allow to accept , just one clarification all the properties need to be defined inside data ? pls confirm
Yes all the variables should be defined in data
Only then they can be accessed using 'this' operator. Also do not confuse computed and methods with the data variables since they can also be accessed using 'this' operator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.