Using a computed property would be a better approach as it allows for more flexibility and reusability. Here's an example of how to implement it:
<template>
<div>
<p class="mb-2">{{ truncatedBody }}...</p>
</div>
</template>
<script>
export default {
props: {
article: {
type: Object,
required: true
}
},
computed: {
truncatedBody() {
return this.article.body.slice(0, 300);
}
}
};
</script>
In this example, we define a computed property called truncatedBody that returns the first 300 characters of the article.body string. We then use this computed property in the template to display the truncated body text.
By using a computed property, we can easily reuse this logic in other components that need to display truncated text. Additionally, it keeps the template cleaner and easier to read.