Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js components : How to truncate the text in the slot element in a component?

Tags:

vue.js

Is there a way to apply a filter to the slot content in a Vue component?

To clarify, I would like to truncate the text included manually in the HTML. For example I would like to transform this:

<!-- In the view --> <my-component> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cumque,  laboriosam quasi rerum obcaecati dignissimos autem laudantium error  quas voluptatibus debitis? </my-component> 

into this:

<!-- Generated component --> <div> Lorem ipsum dolor sit amet, consectetur adipisicing ... </div 

I can't seem to find this information in the documentation.

Thank you.

like image 408
PierreB Avatar asked Jan 28 '16 19:01

PierreB


People also ask

What is Vslot?

v-slot is used on an HTML element. v-slot can only be used on a <template> element or the component receiving the slot. Otherwise, Vue throws a compile error. See Example 1 below. v-slot is used on a component, but the component has another <template v-slot> as a child.

Can we modify props in Vue?

When objects and arrays are passed as props, while the child component cannot mutate the prop binding, it will be able to mutate the object or array's nested properties. This is because in JavaScript objects and arrays are passed by reference, and it is unreasonably expensive for Vue to prevent such mutations.

What scoped slot?

Scoped slots are exactly like regular slots, with the difference that we pass data from the child component to the parent component. This data can then be used inside the slot content.

What is slotProps?

slotProps is just a variable we used to access the props we passed. You can also avoid setting a variable just to hold the props you pass to the child component, by destructuring the object on the fly: <page> <template v-slot="{ dogName }"> {{ dogName }} </template> </page>


2 Answers

The same thing on similar way can be:

in your main.js file:

var filter = function(text, length, clamp){     clamp = clamp || '...';     var node = document.createElement('div');     node.innerHTML = text;     var content = node.textContent;     return content.length > length ? content.slice(0, length) + clamp : content; };  Vue.filter('truncate', filter); 

in your template:

{{data.content | truncate(300, '...')}} 
like image 113
Filip Ajdačić Avatar answered Sep 18 '22 08:09

Filip Ajdačić


You can use a filter to truncate it.

//credit to @Bill Criswell for this filter Vue.filter('truncate', function (text, stop, clamp) {     return text.slice(0, stop) + (stop < text.length ? clamp || '...' : '') }) 

Then give the filter the length you want the string to be

<my-component>     {{'Lorem ipsum dolor sit amet, consectetur adipisicing' | truncate 50 }} </my-component> 

Within the child component, content from a slot is passed through as-is, and isn't available as a variable that you could truncate from the child end.

like image 42
Jeff Avatar answered Sep 21 '22 08:09

Jeff