Output each element of an array in Vuejs2 with a line break between each

I am currently working with vuejs2 and have a select tag where a person is selected, with their address properties directly bound to the element. I need to display the address of the selected person. I attempted to use an array in order to print out the elements with line breaks between them.

{{ 
    [ 
        person.address_1, 
        person.address_2, 
        person.zip_code + ' ' + person.station_city, 
        person.country_name 
    ].join('<br>') 
}}

For example, this should be displayed:

5 Place Charles Béraudier 
Gare de Lyon Part dieu
69003 LYON
France

However, I am facing issues with the formatting of the code. It is displaying the actual code instead of the values.

Answer №1

In Vue, the default behavior of {{ var }} is to escape any HTML content. If you want to output raw HTML, you can utilize the v-html directive:

<div v-html="[ 
    person.address_1, 
    person.address_2, 
    person.zip_code + ' ' + person.station_city, 
    person.country_name 
].join('<br>')"/>

Answer №2

If you're hesitant to use the v-html feature due to its potential risks: https://v2.vuejs.org/v2/guide/syntax.html#Raw-HTML

Rendering arbitrary HTML dynamically on your webpage can pose a significant security threat as it may expose your site to XSS vulnerabilities. It's advisable to only apply HTML interpolation on content that is trusted, and refrain from using it with user-generated content.

An alternative approach is utilizing the <pre> tag: https://www.w3schools.com/tags/tag_pre.asp

To implement this method, you would require a computed function:

<pre>{{ formattedAddress }}</pre>
computed: {
  formattedAddress () {
    return `${this.person.address_1}\n${this.person.address_2}\n${this.person.zip_code} ${this.person.station_city}\n${this.person.country_name}`
  }
}

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

Is there a way to implement absolute imports in both Storybook and Next.js?

Within my .storybook/main.js file, I've included the following webpack configuration: webpackFinal: async (config) => { config.resolve.modules = [ ...(config.resolve.modules || []), path.resolve(__dirname), ]; return ...

Enhance the original array of a recursive treeview in VueJS

After going through this tutorial, I decided to create my own tree view using recursive components in Vue.js. The structure of the input array is as follows: let tree = { label: 'root', nodes: [ { label: 'item1', nod ...

How can I select a checkbox dynamically during runtime?

I am working on a JavaScript code that needs to add the checked option to a checkbox if it has an id or value of 2 at runtime. I have tried the following code, but unfortunately, I am unable to check the checkbox. Do you have any ideas on how to solve th ...

Is it considered bad practice to assign a value to a Vuex property directly in an action rather than using a mutation for the change

Currently, I have a Vuex action that performs a GET request and then assigns the response to a Vuex property: async getUserServers({commit, state, dispatch}, userId){ try { let response = await axios.get("/servers/" + userId) state.serv ...

Screen content of a post request in Node.js

Can this code in node.js + express be simplified? // Code snippet for registering a new participant app.post('/api/participant', function (req, res, next) { var data = req.body; // Ensure only specific fields are uploaded var parti ...

When attempting to trigger a function by clicking a button in Angular 8 using HTTP POST, nothing is happening as

I've been struggling to send a POST request to the server with form data using Observables, promises, and xmlhttprequest in the latest Angular with Ionic. It's driving me crazy because either I call the function right at the start and the POST wo ...

Can you achieve a click event in Vue without using $refs?

I have a template structured as follows: <p @click="handleParagraphClick"> <component v-for="item in items" :is="spanComponent"/> </p> The template for the nested span component looks like this: <span ...

Switching Divs Based on Radio Button Selection in VueJS

How can I display different components based on radio button selection? <input type="radio" name="book" value="One" checked="checked"> <input type="radio" name="book" value="Round"> <div> // initial display when "One" is selecte ...

Executing Controller Actions within a JavaScript Timer

Presenting my latest timer: var eventDate = new Date(Date.parse(new Date) + 3600); function countdown() { var elapsed = Date.parse(eventDate) - Date.parse(new Date()); var seconds = Math.floor((elaps ...

Errors occur when using jQuery Autocomplete alongside Angular HTTP

I have implemented an ajax autocomplete feature for my database using the jQuery-Autocomplete plugin. Below is my current code setup: HTML: <input ng-keyup="searchCustomer()" id="customerAutocomplete" type="text"> Angular $scope.searchCustome ...

"Exploring the Keyboard Shortcuts and Navigation Features of Vuetify 2.x's v-data

Why is it not possible to navigate v-data-tables in Vuetify 2.x when it was a feature in earlier versions like 1.5.x? Is there a straightforward solution for this or an explanation behind the removal of these keyboard commands? ...

Disabling the last control in a formGroup when sorting an array with Angular

I am facing an issue with sorting an array based on a numeric control value inside a formGroup nested in another array: const toSort = [ ['key2', FormGroup: {controls: {order: 2}}], ['key1', FormGroup: {controls: {order: 1}}] ] ...

Uh oh! You haven't set a QueryClient yet. Make sure to use QueryClientProvider to set

My first experience with React Query didn't go as planned when I encountered this error at the beginning of my React app: Even though I added QueryClientProvider to the top of my component tree, I kept getting: Error: No QueryClient set, use QueryCli ...

I am looking for a VueJS component that replicates the functionality of http://jsfiddle.net/NuWna/2/

I am in search of a VueJS Card Component that resembles this particular example: http://jsfiddle.net/NuWna/2/ Struggling to convert the jQuery code into VueJS syntax $(document).ready(function(){ $(".slide").hover(function(){ $(this).find(" ...

Tips for preloading an ENTIRE webpage

I am looking for a way to preload an entire web page using JavaScript in order to have it cached in the user's browser. While I know how to preload images with JS, my goal is to also preload the entire page itself. For example, on my website, there ...

Building multi-dimensional array checkboxes in AngularJS - a step-by-step guide

I am working with a multi-dimensional array and I need to display all the data using checkboxes. The array below contains dietary requirements, and I want to create checkboxes for each entry where the value is true. How can I use ng-repeat to iterate over ...

Using inline SVG in a Vite production build

When utilizing Vite and Vue, my goal is to compile a single JavaScript file without creating an assets directory that includes SVG and GIF files during the build process. I want to achieve this without manually inserting the SVG code in a Vue JS file as a ...

exploring methods to prevent flash of unstyled content (fouc)

Looking for a way to control CSS with a cookie: Want the user's style choice to stick until they change it or the cookie expires Avoid any default styling issues when the user returns Avoid using jquery, libraries, or frameworks Need compatibility w ...

Problem with AngularJS Multiselect checkbox dropdown configuration

In my application, I have a pop-up that includes a multi-select dropdown menu. Here is the code for the Multi-Select Dropdown: <select name="edit_tags" class="form-control" id="advisor_article_tagsx" multiple="" required ng-model="article_selected ...

Retrieve the entire Vuetify component from v-autocomplete

Is there a way to retrieve the entire array element of an Autocomplete item in order to store it in Vuex without needing to store each individual item separately (e.g. first name, middle initial, last name)? I am referencing a similar question that was pre ...