What is the best way to display line breaks within VueJS components?

Is there a way to insert a line break in Vue whenever there is a ↵ character?

I attempted to implement the solution from this Stack Overflow thread, but I am encountering difficulties.

Here is what I have tried:

var app = new Vue({
  el: '#app',
  data() {
    return {
      a: {
        country: "England:↵ - Liverpool↵ - Chelsea↵ - Arsenal↵ - MU↵ - City",
      },
      testObj: "",
    };
  },
  computed: {
    jsonFunc() {
      return (this.testObj = this.a.country.replace(/↵/g, "<br/>"));
    },
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div>
    Vue
    {{ jsonFunc }}
  </div>
</div>

Answer №1

Implement the v-html directive in Vue.js

<template>
  <div >
    Vue
    <span v-html='htmlFormattedText'></span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      originalText: {
        country: "England:↵ - Liverpool↵ - Chelsea↵ - Arsenal↵ - MU↵ - City",
      },
      processedText: "",
    };
  },

  computed: {
    htmlFormattedText() {
      return (this.processedText = this.originalText.country.replace(/↵/g, "<br/>"));
    },
  },
};
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue specifically with Android 6 WebView - jQuery 1.9.1 where a RangeError is thrown due to the call stack size being exceeded

An error message "Uncaught RangeError Maximum call stack size exceeded" is causing a web application to fail in the jQuery-1.9.1 extend() function, but strangely it only occurs on Android 6. The application runs smoothly on all other platforms such as Des ...

Immersive image display

Currently on my website, I have a table displaying 9 images with descriptions. I'm looking to enhance user experience by allowing them to click on an image and view it in a larger format like a gallery without disrupting the layout of the page. This ...

Creating the illusion of a typewriter effect while adjusting container sizing

I created a unique typing animation that displays text as if it's being typed on the screen, erased, and replaced with new text. While this effect works well, it's causing an issue where the width of containers below it also adjust based on the s ...

Combining Angular 2.0 within Angular 1.x: Elevating your module

Currently, I am attempting to incorporate an Angular 2.0 component within an Angular 1.x application (for experimentation and learning purposes). Upon further examination, I have observed that this can be achieved by referencing the Angular2 upgrade modul ...

Preventing Body Overflow Without Affecting Iframe Overflow in Meteor.js

Currently, I am working on a project using Meteor for a Point of Sale (POS) system. For those unfamiliar with it, Meteor is a framework that allows for the use of JavaScript, jQuery, and various other web app scripting languages. The goal of this applicati ...

Is jQuery's .html() behaving incorrectly when setting content?

Allow me to present an example that will help explain the issue more clearly. http://codepen.io/anon/pen/EVpYXm If you take a look at the initial <html>, you'll see that it displays the text: "This is the old html!" The content is set base ...

Issue updating user information in Laravel 7

I've been searching for a solution, but I haven't been able to update the "status" field in the users table. I recently added two additional fields to Laravel's default user table. Here is the migration code I used: public function up() { ...

The functionality of Angular.js ajax and apply is experiencing technical difficulties

As I venture into the world of angular.js, I am facing a challenge with displaying the correct content on my website. The pages' content is fetched via AJAX (currently from static data, but eventually from a database). When I place a block with the ng ...

Display sibling element upon hovering with AngularJS

Within a single view, I have multiple instances of repeated content elements. Each content element contains an anchor element. My goal is to toggle a class on a sibling element within that specific content element when a user hovers over the anchor. For c ...

Error: Model attribute missing in Adonis JS v5 relationship

Recently, I started diving into the Adonis framework (v5) and decided to build a todo list api as part of my learning process. However, I'm facing an issue concerning the relationship between the User and Todo entities. Let me show you the models fo ...

What is the best way to trigger a function to return a property value within the same React component file when using the map function?

Currently, I am dealing with a unique data structure that resembles the following: allPersons:[ { discoveriesByType:[ {type:'car',pending:0,confirmed:10, discoveries:[{data...}]}, {type:'truck',pending:1,confirmed:25 ...

Encountering an error in React: Unable to access property 'elements' of undefined due to incorrect passing of the event object

Struggling to make a form submit text to send via axios to the backend API in React. Following an outdated tutorial and using antd library for the form template. Here is the current form code with some console logs to debug: import React, { useState } fr ...

javascript create smooth transitions when navigating between different pages

As a newcomer to JS, I am currently working on creating a website with an introduction animation. My goal is to have this animation displayed on a separate page and once it reaches the end, automatically redirect to the next webpage. <body onload="setT ...

`Switching between two buttons: A guide`

Here is the codepin: https://jsfiddle.net/magicschoolbusdropout/dwbmo3aj/18/ I currently have a functionality in my code that allows me to toggle between two buttons. When I click on one button, content opens and then closes when clicked again. The same b ...

Having trouble connecting an array of objects to a Vuetify data table?

My Vuetify data table is not displaying any data in the body, even though I can see the Firestore data in the console log. Could it be because my items array has more data points than there are headers in the table? Vuetify Component <template> < ...

Ways to join two if statements together using the or operator

Below is my code attempting to check if either child elements H1 or H2 contain text that can be stored in a variable. If not, it defaults to using the parent element's text: if($(this).children('h1').length){ markerCon[markerNum] = $(th ...

Is there a way to retrieve data from a sealed JSON object using JavaScript?

The data is being fetched from the API and here is the response object: { "abc": [{ "xyz": "INFO 1", "pqr": "INFO 2" }, { "xyz": "INFO 3", "pqr": "INFO 4" } ] } We are lookin ...

Ways to showcase information in real-time based on user interaction

I've created a form (php file) that enables users to input investment information into the database. They can choose investments from a dropdown list (previously entered into the database), add additional details, and submit them to the database. I&ap ...

Choose2 generateUniqueEntry identification from submission

I am currently using select2 for a tagging input, but I am facing some challenges when it comes to handling the creation of new tags and retrieving the new tag IDs back into the select2. This issue is closely tied to another question on this topic which c ...

Optimal strategies for integrating Vue 3 application into a Laravel website

Current scenario: The website is currently running on Laravel 7 with Spark 11, which includes integrated Vue 2 functionality. New scenario: A new Vue3 application has been developed and needs to be incorporated into the Laravel website to avoid any cros ...