Using the <pre> tag with Vue.js in v-html allows for displaying pre

When using v-html to display content retrieved via AJAX, I encountered an issue with the "pre" tag not rendering as expected.

Here is my component setup:

<div v-html="content"></div>

For example, the content includes:

<h1>Title</h1>
<pre>
    <p>Hello</p>
</pre>

The current output is:

Title

Hello

But ideally, I would like it to be displayed as follows:

Title

<p>Hello</p>

I am unsure if this desired format can be achieved with v-html. Any suggestions or solutions?

EDIT :

The content is generated from a form (wysiwyg) and encoded with he.js before saving. The encoded version looks like this:

&lt;h1&gt;Title&lt;/h1&gt;
&lt;pre&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;/pre&gt;

After retrieval via AJAX, I decode it with he.js to get:

<h1>Title</h1>
<pre>
    <p>Hello</p>
</pre>

Answer №1

Why don't you give this a try:

<h2>Heading</h2>
<pre>
    &lt;p>Greetings&lt;/p>
</pre>

If you want your HTML content to display correctly, you'll need to escape the tags or keep the pre tag separate from the rest of the content.

In my opinion, I would approach it like this:

<section>
<h2>{{data.heading}}</h2>
<pre>
    {{data.content}}
</pre>
</section>

In your code, make sure to include a data object with the necessary information:

data: {
 heading: 'Heading',
 content: '<p>Greetings</p>'
}

Consider if the structure of your content may change in order to determine the best solution for your specific case.

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

When setting up Vue.js for unit testing, the default installation may show a message stating that

Recently set up a fresh Vue project on Windows 7 using the VueJS UI utility. Unit testing with Jest enabled and added babel to the mix. However, when running "npm test" in the command line, an error is returned stating 'Error: no test specified' ...

The functionality of Highcharts-more.js is not functioning properly within a project set up using vue-cli

Recently, I have been working on a vue-cli project and attempting to create a polar chart similar to the one shown here: To achieve this, I needed to install and import the highcharts and highchart-more libraries using npm. However, upon importing both fi ...

Vue CLI Plugin Electron Builder displays a completely empty screen after compiling

After building my electron app using this specific plugin, I encountered a frustrating issue where the installed package would only display a blank, white screen. Despite setting up the window to open dev tools in the built version, inspecting the page rev ...

The ideal structure for a Vue app

As a newcomer to Vue, I've been exploring different guidelines on how to use it effectively. In one resource here, the project was structured with separate directories for assets, components, routes, services, and views within a 'src' direct ...

Tips for resolving the error message: 'The "path" argument must be a string. Received type undefined' that occurs when executing 'vue add vuetify'

After successfully creating a new app using 'vue create agenda', I proceeded to cd into the project folder and run 'vue add vuetify' to integrate Vuetify. However, I encountered an unexpected error. I attempted to search for solutions ...

Guide to utilizing Terser in conjunction with webpack

I am currently using Webpack 6.10.2 in combination with Vue 3.9.3. I encountered an issue with Uglify.js when running npm run build due to its inability to handle ES6 code. To work around this problem, I followed the recommendation of removing Uglify fro ...

Why does Request-Body(req.body) display a value while Request-QueryParams(req.queryParams) returns null?

Using vuejs-axios, I successfully transferred client-side data to the server-side using Java with SparkJAVA Framework to manage the request-response cycle. The following code snippets demonstrate how Form-Data is posted from vuejs to java. const formData ...

Utilizing Vue.js to fetch information from a post API in Node.js

I'm currently working on developing a post API in Node.js, with Vue.js as my frontend framework and using vue-resource to interact with APIs. Here is the submit function I have implemented on the frontend: validateBeforeSubmit() { var self = this ...

Guide to resolving a blank webpage issue post running 'npm run build'

I am currently in the process of working on a project that involves Vue and Firebase. Unfortunately, I have encountered an issue where my development server is no longer rendering new routes from my Vue router after building and deploying to production. F ...

Transmitting data from express server to vue.js interface

Hey there, I am facing a bit of a complex situation and could really use some help. Here's what I've got: an application split into server-side using node/express and client-side with Vuejs. The issue arises when I try to create a user on the ba ...

Encountering issues with npm installation on a Linux operating system

Embarking on a new journey learning Vue.js, I find myself in unchartered territory with Node.js. Initially, when I cloned the repository for my course and followed the setup instructions, everything ran smoothly without any issues. However, a blunder on my ...

Leveraging Environment Variables in Vue.js

I've been diving into the official documentation, but haven't come across any information regarding environment variables. While there are community projects that offer support for this feature, they seem a bit excessive for my needs. I'm cu ...

Upon executing npm install in my Laravel project, an error message promptly appears stating: "npm WARN deprecated [email protected]: gulp-util is deprecated - replace it."

Recently, I set up a brand new Laravel Project. After executing the command: php artisan preset vue, when I tried running npm install, an error occurred that stated: npm WARN deprecated [email protected]: gulp-util is deprecated - replace it, followi ...

unable to transfer Vuex store information to the graph

Currently, I am facing an issue with passing my vuex store data into my apexcharts graph. Despite my efforts, it seems like the data is not being displayed correctly. Can anyone provide guidance on what I might be doing wrong? My main objective is to updat ...

What is the best method for ensuring image orientation is displayed correctly?

When utilizing multer node and express for uploading images to my application, I've noticed that some of the images appear rotated 90 degrees once they reach the client side. What could be causing this issue, and how can I resolve it? Just to clarif ...

What causes the ignoring of config.proxy in axios requests while working on a webpack project?

My objective I am aiming to make a request using [email protected] with full efficiency through an http proxy (squid). My project is built on Vue and uses the webpack template. I initialized it with vue init webpack proxytest The challenge Despite ...

Node.js vulnerability labeled as CVE-2021-38628

Description: Enhance security measures by disabling the use of TLSv1.0 protocol and transitioning to a more secure option like TLSv1.2. To perform a manual test, utilize the following openssl commands: openssl s_client -connect ip:port -tls1. If succes ...

Vue specifies the location of the index.html file

I'm new to working with Vue and node, and I'm attempting to insert a global value in my project by placing my index.html in a public directory. After creating the project, I noticed that the public src folder was not generated, but I could still ...

The installation of vue-cli failed due to permission issues with error code EPERM in npm

I keep encountering an error message when I try to run npm install -g vue-cli: npm ERR! path C:\Users\End User\AppData\Roaming\npm\node_modules\vue-cli\node_modules\nan\package.json npm ERR! code EPERM ...

Converting a string into an array of JSON objects

I am currently attempting to send data to my NodeJS server using the HTTP protocol (vue-resource). The goal is to send an array of JSON objects structured like this : [{"name":"Charlotte","surname":"Chacha","birth":"2000-04-02"},{"name":"Michael","surname ...