Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have a vue component whose template only contains the text of a property?

Is there a way to have a vue component which has a template area that only contains plain text? The problem I face with the current solution (using a div to wrap it around the text) is that I recursively concatenate these components with other text fragments in one line which often leads to jumps to the next row of the parent container because there is not enough space left on the right side of the text fragment that came before the current one. Consequently, text paragraphs which should look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer molestie sit amet mauris sed sollicitudin. Curabitur non quam at nibh venenatis facilisis. Integer vitae augue vel quam condimentum ultricies. Mauris consequat elit vitae sollicitudin auctor. Donec volutpat velit nulla, vitae fermentum erat aliquet non. Mauris congue nibh eget justo sodales luctus.

Resultingly look like follows:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer molestie sit amet mauris sed sollicitudin.

Curabitur non quam at nibh venenatis facilisis. Integer vitae augue vel quam condimentum ultricies.

Mauris consequat elit vitae sollicitudin auctor. Donec volutpat velit nulla, vitae fermentum erat aliquet non.

Mauris congue nibh eget justo sodales luctus.

Under consideration that there are 5 text fragments in the above example, respectively.

This is how the said component is implemented with comments inside the template tags to concretize my issues:

<template>
  <div>{{element.text}}</div> <!--  This is how I can do it to this point-->
  {{element.text}}            <!--  This is what I would like to do-->
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import TextElement from '@/models/elements/Text'

@Component
export default class TextWrapper extends Vue {
  @Prop() element!: TextElement
}
</script>

<style scoped>
</style>
like image 708
nymvno Avatar asked Oct 12 '25 01:10

nymvno


2 Answers

Simple answer is no, render functions/templates require a root element. Instead of using a <div> you can use an inline element such as a <span> or style the div with the css property display: inline to work around this issue.

like image 90
JaredMcAteer Avatar answered Oct 14 '25 14:10

JaredMcAteer


Vue component templates should have root element that exists in DOM. Commonly div or span wrapper elements are used. This is rarely a a problem because root element can be styled to fit the layout.

There's vue-fragment plugin that provides the functionality of React.Fragment in Vue. It basically unwraps root element on mount. Since it's a hack, it may not work in all situations as expected.

Render function can be used to render text nodes with _v internal method. That it uses undocumented API suggests that it's a hack as well.

like image 37
Estus Flask Avatar answered Oct 14 '25 13:10

Estus Flask