Using Vue.js to inject plain text into a Vue component from HTML

I am currently in the process of transitioning code from ASPX to VUE.JS.

In the previous system, there was a feature that allowed plain text to be injected into HTML (such as messages with text, images, links, and inputs). However, in VUE, the injected HTML is not functioning properly...

To illustrate the root issue, I have created a demo.

Explore CodeSandbox Example

Essentially, we are attempting to inject HTML into a VUE component similar to regular HTML... but it also contains JavaScript code within the plain HTML.

Here is an overview of the problem: https://i.sstatic.net/Uiw2s.png

Any suggestions on how to navigate around this issue? The plain text may involve various JavaScript functions (potentially over 50 different scenarios).

Answer №1

When trying to execute JavaScript with injected code like that, it may not work as expected. To check this, you can test by adding console.log('test') inside the <script> tag.

To fix this issue, you can add a regex function after your replace function, which will evaluate every script tag.

codeBlock.match(/(?<=<script>).*?(?=<\/script>)/gms).forEach(function (match) {
  window.eval(match);
});

You can also find an improved version of your demo with the solution implemented here: https://codesandbox.io/s/vue-injection-problem-forked-wigzr?file=/src/components/HelloWorld.vue

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 developing a node.js project using VMWare's shared folder, how can you manage the node_modules folder?

My current project needs to run on Linux, but I am using a Windows computer. This means I have to use a VM. Despite wanting to use WebStorm, I am avoiding JB Gateway due to its numerous bugs. My solution was to utilize the VMWare share folder function. Ho ...

Having Trouble Dividing Routes into Individual Files in Express Version 4.0

I need to redirect routes ending with /api to a file called manager.js, which will then route it to /me. For example, a request to /me should be redirected to /api/me. In Express 3.x, splitting routes into separate files was simple, but I'm facing ch ...

Using local file paths in v-lazy-image does not function properly

When trying to use the v-lazy-image plugin for my page banner with local image files, it does not seem to work. <v-lazy-image srcset="../../../assets/images/banners/Small-Banner.svg 320w, ../../../assets/images/banners/Big-Banner.svg 480w&quo ...

transform the stationary drawing to the top and left position at coordinate (0,0)

Exploring canvas has led me to encounter a puzzling issue with the translate method. While working on this fiddle http://jsfiddle.net/claireC/ZJdus/4/, my goal was to have the drawing move and bounce off all four walls. However, I noticed that it would no ...

Passing Data from Child to Parent Components in ReactJS

I'm new to React and JavaScript, currently working on a website. I've encountered an issue with passing data between components from child to parent. Here's the scenario: In my App.js script, I'm using react-router-dom for routing. I ...

Unable to configure AngularJS inputs to have a blank default value

I am facing a peculiar issue where I am unable to initialize my input fields as blank/empty. Even after clearing the cache and performing a hard reload, Google Chrome seems to be auto-populating them with older cached values. Here is the current view: ht ...

Jade not responding to AngularJS click directive function

This tutorial is inspired by the MEAN Stack demo: Mongo, Express, AngularJS, and NodeJS I am attempting to incorporate a 'delete' method into my controller in the Jade template as shown below: characters.jade script function CharactersCont ...

Create a global SCSS file in ReactJS that stores all variables, mixins, and more

Looking for a solution to manage multiple components in your SCSS project efficiently? Consider creating a central file that houses all your variables and mixins, making them globally accessible without the need to manually import the file into every SCSS ...

The custom filter in AngularJS fails to activate when a click event occurs

I am trying to customize the filtering of my table data based on selected conditions from a dropdown menu. I have created an AngularJS custom filter and passed all the necessary parameters. The desired functionality is that if no conditions are selected, ...

How can we incorporate spans into child elements using JavaScript?

Recently, I encountered a problem with a code that is supposed to add a span if it doesn't already exist on certain elements. However, the current implementation only adds the span to one element and not others because the code detects that it already ...

Implementing a dynamic function with jQuery Tokenize's change event

I'm currently facing an issue with triggering the on change event on the select box from jQuery Tokenize. Below is the code snippet I am working with: <select id="tokenize" multiple="multiple" class="tokenize-sample"> <option value="1"&g ...

Learn how to use a function in Google Maps to calculate and display distances

I have a script obtained from this link here and I am trying to create a function that returns the distance. This is my current script: var calcRoute = function(origin, destination) { var dist; var directionsDisplay; var directionsService = ne ...

What are the steps to add 8 columns to the second row in my table?

this is an example of HTML code that showcases a table structure with multiple rows and columns. You can view the full code snippet here. The table includes different sections, such as section1, section2, section3, and section4 in the first row (tr). In t ...

Does JavaScript automatically round large numbers?

I have a simple array: myArray = [{"egn":79090114464},{"egn":92122244001},{"egn":10005870397643185154},{"egn":10000330397652279629},{"egn":10000330397652279660},] However, when I append the values from thi ...

React:error:unexpected punctuation

I encountered an issue with this code within the render() function, specifically on the line where I am returning a value and it shows an unexpected token error. Here's the snippet of the code. class PuzzleGame extends React.Component { construct ...

The issue arises when d3.scaleLinear returns NaN upon the second invocation

My journey with d3.js is just beginning and I'm taking it slow. Currently, I'm focused on creating a bar chart where data is loaded from a json file. When I click on the bars, the data changes to another column in the json. This is how my json f ...

Struggling with Firebase website build failure on GitHub Actions because of node-sass?

Lately, I've been successfully deploying my Vue SPA to Firebase Hosting using the GitHub actions provided in the setup. However, all of a sudden, these actions are failing and I'm unsure of how to resolve the issue. Below is my .yml action file: ...

Using AngularJS to apply filters to JSON data

I'm having trouble filtering a JSON array. Here's an example of what my JSON array looks like: vm.users = [{ "fname": "Antoan", "lname": "Jonson", "Address": "Address1" }, ... ] How do I filter by last name starting with a specific term (e.g. & ...

When the modal is closed, the textarea data remains intact, while the model is cleared

My challenge involves a simple modal that includes a text area. The issue I am facing is resetting the data of the textarea. Below is the modal code: <div class="modal fade" ng-controller="MyCtrl"> <div class="modal-dialog"> <d ...

bespoke filter designed to conceal any negative figures

I am trying to implement a feature where a text box will display nothing if the ng-model contains a negative value. I want the ng-model to remain unchanged while ensuring that negative values are not displayed. I am looking for a custom filter to achieve t ...