Deciphering HTML within a Vue attribute: Tips and tricks

I'm having trouble converting some code to Vue. There seems to be a rendering difference in the alt attribute of an image, and I can't figure out how to interpret the html within the attributes. I've searched for solutions but haven't found any relevant topics on this issue. Do you have any suggestions?

In my examples, I intentionally omitted the src to focus on the alternative text.

Vue.component('first-image', {
  inheritAttrs: false,
  template: '<div class="container-image"><img v-bind="$attrs"></div>'
});

Vue.component('second-image', {
  template: '<img>',
});

new Vue({
  el: '#app',
  data() {
    return {
       alternativeText: '1&nbsp;000€',
    };
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<!-- Without VueJS -->
<img alt="1&nbsp;000€">

<br>
<br>

<!-- With VueJS -->
<div id="app">
  <first-image :alt="alternativeText"></first-image>
  <second-image alt="1&nbsp;000€"></second-image>
</div>

Answer №1

If you want to implement the following code snippet, here's how:

Vue.component('first-image', {
  inheritAttrs: false,
  template: '<div class="container-image"><img v-bind="$attrs"></div>'
});

Vue.component('second-image', {
  template: '<img>',
});

new Vue({
  el: '#app',
  data() {
    return {
       alternativeText: '1&nbsp;000€',
    };
  },
  methods: {
    decodeHtml(html) {
      const txt = document.createElement("textarea");
      txt.innerHTML = html;
      return txt.value;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<!-- Without VueJS -->
<img alt="1&nbsp;000€">

<br>
<br>

<!-- With VueJS -->
<div id="app">
  <first-image :alt="decodeHtml(alternativeText)"></first-image>
  <second-image alt="1&nbsp;000€"></second-image>
</div>

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 a child component is updated, React does not automatically re-render

My goal is to pass the email from the SigninForm back to the App component and trigger a re-render when the email is updated. I attempted to follow the structure outlined in a previous question on Stack Overflow, but unfortunately, I couldn't get the ...

exploring XML documents

With around 250,000 XML files, each named with a UUID, I am looking for the most effective way to perform a full text search on these files and identify the UUID of the matching ones. What would be the optimal approach for indexing them in a nodejs environ ...

trouble encountered while parsing JSON information using JavaScript

[ [ { "Id": 1234, "PersonId": 1, "Message": "hiii", "Image": "5_201309091104109.jpg", "Likes": 7, "Status": 1, "OtherId": 3, "Friends": 0 } ], [ { "Id": 201309091100159, "PersonI ...

Navigate to the homepage section using a smooth jQuery animation

I am looking to implement a scrolling feature on the homepage section using jquery. The challenge is that I want the scrolling to work regardless of the page I am currently on. Here is the HTML code snippet: <ul> <li class="nav-item active"> ...

Encountering issues with reading undefined properties while working with react-chartjs-2 and chart js

Having trouble with react chartjs errors? Visit the link for more details https://i.stack.imgur.com/lI2EP.png The versions I'm using are ^3.5.0 for chart.js and ^4.0.1 for react-chartjs-2 Tried downgrading to version 2 but it didn't solve the ...

Ways to generate an array containing the headings from a list using typescript?

How can I extract the headers of objects in an array in TypeScript? let data = [{name: "A", skills: 50, result: 80}, {name: "B", skills: 40, result: 90}, {name: "C", skills: 60, result: 60}]; let headers = Ob ...

Using Node.js to download files with like wget, unzip them, and convert them to JavaScript without saving to

Currently, I am working on a script for a nodejs/express server-side application using the libraries request, unzip, and xml2js. The goal of this script is to fetch a zip file from a specified URL, extract an XML file from it, and then parse that XML into ...

The concept of "Object Reference Pattern" in the world

Currently, I am working with a single object: var callback = { onValueChange: function () { }, onTabPressed: function () { }, onFocus: function () { } }; In my webpage, I have various editors such as textEditor and numericEditor, and I bind t ...

Converting JSON data to an array using an Ajax request

I am currently working on an HTML project where I have the following code: <div id="main"> <div id="blogcont"> <p></p> </div> <button class="nvgt" id="prev" >Previous</button> <button ...

Ideal Setup for OctoberCMS using Laravel, Vue.js, and Tailwind CSS

As a newcomer to OctoberCMS, I've noticed there are limited tutorials available online. I'm curious about the best setup or possible configurations that can be used with this web technology. We have a specific project requirement which dictates ...

Encountering a white screen and MIME Type Error when attempting to deploy a Vite app on GitLab Pages

I am facing an issue while trying to deploy my Vite application on Gitlab Pages. The page only displays a blank screen and the following errors are shown: The CSS and JS files from the static directory cannot be loaded due to MIME type mismatch (X-Content ...

Which one should I prioritize learning first - AngularJS or Laravel?

As a novice web developer, I am embarking on my first journey into the world of frameworks. After much consideration, I have narrowed it down to two options: AngularJS and Laravel. Can you offer any advice on which one would be best for me to start with? ...

javascript - substitute the dash (hyphen) with a blank space

For quite some time now, I've been on the lookout for a solution that can transform a dash (hyphen) into a space. Surprisingly, despite finding numerous responses for changing a space into a dash, there seems to be a scarcity of information going in t ...

Is it possible to selectively render a page or component in Next.js 13 based on the request method?

Is there a way to conditionally render a page based on the request method in Nextjs 13? For example, when registering a user, can we show a signup form on a get request and display results on a post request? With all components now being server components ...

express includes a 500 error due to the .html extension for the view engine

I have an express app where I've configured my views to use HTML, but behind the scenes, I'm actually utilizing the ejs engine in order to maintain the .html extension. Here is how it's currently set up: app.set('views', path.join ...

Migrate the JavaScript variable created with 'passport' to an Express router

In my quest for authentication, I created the essential passportAuth.js file: module.exports = function(passport, FacebookStrategy, config, mongoose, fbgraph){ passport.serializeUser(function(user,done){ //to make user reference available across pages ...

The alignment of Bootstrap v4.5 checkbox is limited as it cannot be positioned horizontally or vertically when placed next to an input field

As stated in the title: Bootstrap's checkbox is having trouble aligning Horizontally or Vertically when placed next to an input. Error Displayed: https://i.sstatic.net/hNHpm.png I have experimented with various solutions, but none have provided sat ...

`json object allows for category selection in options`

Hello (please excuse my English), I have the following script: <script type="text/javascript"> $(document).ready(function() { var idPlato = decodeURI(getUrlVars()["idPl"]); var url = "http://localhost/plato-datos.php?idPla="+idPl ...

Is there a way to access hover effect information in Atom editor similar to how it appears in VScode?

Is there a specific plugin required in Atom to display information when hovering over variables, objects, or functions similar to intellisense? VSCode does this automatically, but I am looking for the same functionality in Atom. https://i.stack.imgur.com/ ...

Error: The function this.$.AjaxPost.go is missing or not recognized in the core-ajax module

Recently, I created an HTML page for handling Password Reset functionality on our website. We have decided to make POST calls to the server and are implementing it using polymer. Below is a snippet of the code: <dom-module id="user-password-reset"> ...