Can Vue/JavaScript code be transmitted within an object to a different component?

The scenario involves a parent and child Vue components. In this setup, the child component transmits data to the parent component via a simple object emitted using the emit method.

Data in the child component:

   const Steps [
      {
            sequence: 1,
            name: "Personal",
            description: `<p>Enter your name and phone number</p>`,
          },
      {
            sequence: 2,
            name: "OTP",
            description: `<p>An OTP code has been sent to you. Resend code</p>`,
          },
    ]

const SelectedStep = ref( 0 ); 
const ActiveStep = ref( {} );  

SelectedStep.value += 1; 

ActiveStep.value = Steps.find(step => {
        return step.sequence === SelectedStep.value
      })

emit('SelectedStep', ActiveStep.value);

Based on the selected sequence, the corresponding object from the Steps array is loaded into ActiveStep and then sent to the parent component.

However, the issue arises when trying to render a link within the object with sequence: 2 in the description. Ideally, it should be a clickable link that triggers a function to resend the code. An example of the desired format:

{
        sequence: 2,
        name: "OTP",
        description: `<p>An OTP code has been sent to you. <a v-on:click="resendOTP">Resend code</a></p>`,
      },

Unfortunately, the v-on:click attribute is not being interpreted and remains as plain text in the HTML output.

The parent component functions as a view that utilizes the child component:

    <header>
    <h1>{{ActiveStep.title}}</h1>
    <div v-html="`${ActiveStep.description}`">{{ActiveStep.description}}</div>
    </header>
    <div>
          <div class="content">
            <Component-Signup v-on:SelectedStep="updateActiveStep"/>
          </div>
       </div> 
    
    <script>
    import ComponentSignup from "../../components/Signup.vue"
    
    export default {
    components: {
    "Component-Signup": ComponentSignup
  },
    setup() {
    const ActiveStep = ref({});
        
    function updateActiveStep(SelectedStep) {
          ActiveStep.value = SelectedStep 
         }
    
    return {
          updateActiveStep,
          ActiveStep
        }
    }
}
    </script>

How can the desired behavior be achieved in this context?

Answer №1

It appears that the description you are using contains HTML code, which means that interpolation ({{ }}) will not render it as expected; instead, it will be displayed encoded.

To render raw HTML, you can utilize the v-html directive. More information on this can be found here.

However, please note that v-html is only suitable for HTML content. Vue-specific functionalities like v-on will not work in this context. Refer to the official documentation for more details:

Keep in mind that v-html cannot be used to create template partials, as Vue is not a string-based templating engine. Components are the recommended approach for UI reuse and composition.

If needed, consider creating separate components for each step and utilize the is attribute to display the appropriate component based on the current step in your application.

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

In a functional component in Typescript, what data type should be used for a parameter that accepts an array of objects?

const FormData = ({ dataArray }: object[]): JSX.Element => { console.log("Array of Objects",dataArray) //This is a large form component.... }); The dataArray contains multiple objects, how can I specify a specific type for these components ...

Identify and alert when the download has finished

Imagine having a drive link to download a zip file. Upon clicking the link, the download begins in the browser. After the download finishes, I would like to send an email notification to the user. Is this achievable? I am using a .NET application (C#) wi ...

Populate HTML form with return values using jQuery AJAX

I am struggling with retrieving values from jQuery/AJAX and displaying them in an HTML form within the index.php file. This is what my index.php looks like: <script type="text/javascript"> $(document).ready(function () { $('#display'). ...

Is there a way to integrate a calendar feature within an Angular 2 application?

I am brand new to Angular 2 and have a question: I need to integrate a calendar into a page where users can add events. Something similar to the following example: I'm unsure if the angular material calendar project is designed for Angular 2 or for ...

Looking for a way to add interactivity to this canvas rectangle?

ctx.fillStyle = "#9b958c"; ctx.fillRect(sampleRectX, sampleRectY, sampleRectW, sampleRectH); ctx.fillStyle = "#fff"; ctx.fillText("Click here to play again.", sampleTextX, sampleTextY); This clickable rectangle has been giving me some trouble. While I fou ...

Tips on transitioning a Node.js application from JavaScript to TypeScript incrementally

I have a JavaScript node application that has grown quite large and I am considering migrating to TypeScript for faster development and easier code maintenance. I have installed TypeScript along with the node and mocha types using the following commands: ...

Strategies for Mitigating Duplicate Requests in AngularJs with just one click

I am facing an issue with my AngularJS application where clicking on a link triggers the API call twice. I'm not sure why this happens and how to fix it. My service: SomethingService function getData() { return apiSettings.getApiInformation().t ...

A guide on toggling classes in Ember

I've been exploring ways to toggle a class within a component. The component I'm working with has two states: active and inactive. To activate it, I need to add the class active. Currently, I'm using jQuery's addClass and removeClass ...

How can you use $_REQUEST in PHP to fetch an array of inputs for database insertion?

I have a webpage where I am utilizing AJAX to transfer inputs to a PHP file for database entry. The following is my JavaScript code: var pageLoaded = function () { var submitButton = document.getElementById("submit"); if (submitButton) { submitButton. ...

Is there an Angular counterpart to Vue's <slot/> feature?

Illustration: Main component: <div> Greetings <slot/>! </div> Subordinate Component: <div> Planet </div> Application component: <Main> <Subordinate/> </Main> Result: Greetings Planet! ...

An Array of objects is considered NULL if it does not contain any values before being iterated through

Working with Files in TypeScript (Angular 8) has led me to Encode the files in Base64 using the code below: private async convertToBase64(evidences: Array<EvidenceToDisplay>): Promise<Array<EvidenceToDownload>> { const results: Arr ...

Ajax seems to be interacting with an empty session variable

I am facing some issues with a piece of code that I've written. Here's the function in question from my PHP class: public function generalSettings(){ $totalPrice = 0; foreach($_SESSION['items'] as $key => $value){ ...

The data retrieved from the backend is being stored in an object, however, it is not directly accessible through

After receiving data from the backend and storing it in an object, I am facing an issue where trying to print it using object properties shows undefined values. Here is a snapshot of my output: view image here Snippet of my code: this.documentService.getD ...

Error: The constructor for JsSHA is not valid for the TOTP generator

Attempting to create a TOTP generator similar to Google's timed-based authenticator using the React framework. I am utilizing the bellstrand module for TOTP generation, but I am encountering issues when trying to import it into my React code. Here is ...

What is the best approach to structure a React project in which a component's rendering changes with each instance?

I am facing a challenge with rendering a component that relies on multiple AJAX calls to complete. These AJAX responses return different data each time, and I want to implement a button that will trigger the re-rendering of this random component every time ...

When using react-router-dom, a blank page may appear for routes that contain more than one path segment followed by a single forward slash "/"

Whenever I attempt to set up a route with more than one "/" (e.g. "/testing/test"), React simply displays a blank page instead of showing the Test component along with the navigation bar and footer. Interestingly, the route "/testi ...

Ensure that the GraphQL field "x" with type "[User]" includes a selection of subfields. Perhaps you intended to specify "x{ ... }" instead

I am encountering an issue while attempting to execute the following simple query: {duel{id, players}} Instead, I received the following error message: Field "players" of type "[User]" must have a selection of subfields. Did you mean & ...

Button Click Does Not Trigger Bootstrap Modal Display

This is a sample Bootstrap document that includes JS and CSS files to display a list properly in Bootstrap. However, the Modal does not appear when clicking on Launch demo modal. No JavaScript errors are displayed in the Console. Can you help troubleshoot ...

Why am I unable to make a selection in the dropdown menu?

Can anyone help me figure out why I am unable to select an option from the material ui library and display it in the state? Has anyone else encountered this issue? For a detailed code example, check here import React from "react"; import ReactDOM from " ...

Creating a React.js NavBar: Can we refer to another component's HTML code? (Exploring the idea of a navigation bar that links to components within the same route)

My goal is to create a navbar for my personal website application that references different content sections (such as About, Skills, etc.) all on a single page route. However, I am running into some challenges. I can easily reference markup with ids/classe ...