0

I've been trying to truncate text within a component and it works, sorta.

I am currently using the following code to truncate the text;

filters: {
  truncate(string, value) {
    return string.substring(0, value) + '…';
  }
}

Then the truncate is called with the following line;

<p>{{ excerpt | truncate(77) }}</p>

I'm not sure why this is happening since the filter(truncate) and where it's being called is within the same component, then both of the pages being the root page and the other page calls the component the exact same way.

This seems to work for one page being the root page however, it doesn't work on other pages with displaying the following error;

enter image description here

1 Answer 1

1

Your code is fine. It just appears the string isn't defined immediately. You can add a guard like:

(string || '').substring(0, value) + '…'

Though, it might be better to not render that part of your code until things are ready. You can do something like this (assuming you're working with an object)

<div v-if="post">
  {{ post.excerpt | truncate(3) }}
</div>
<div v-else>
  Post is loading...
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, after you mentioned that it could just not be loading just yet along with the v-if statement; It gave me feedback that it can't find the excerpt.
No problem at all!
@Bill Criswell Maybe you can help me. Look at this : stackoverflow.com/questions/52584451/…

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.