Vue: Initializing data as a string with hyperlink

I am working with an array of objects called boxData within my initial data. Is it possible to transform the word "here" into a hyperlink that I can later reference in relation to boxData?

data() {
    return {
        boxData: [
            {
                body: "This is where you'll find the link."
            },
            {
                body: "Just regular text here."
            }
        ]
    }
}

Child component

<div v-for="(box, index) in boxData" class="box">
    <div>
         {{ box.body }}
    </div>
</div>

Answer №1

In an ideal scenario, objects containing a link would possess a property named url. If all the links are intended to display as "This is the link here", you could use the following approach:

<div v-for="(box, index) in boxData" class="box">
  <div>
    <template v-if="box.url">
      This is the link <a :href="box.url">here</a>.
    </template>
    <template v-else>
      {{ box.body }}
    </template>
  </div>
</div>

This way, there is no need to include "This is the link here" within any of the objects.

Answer №2

To include the data in a hyperlink, follow these steps:

<a :href="boxData.body">Here</a>

This method is much improved.

<router-link :to="boxData.body">Here</router-link>

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

The compilation of the module has encountered an error with the PostCSS loader. There is a SyntaxError at line 2, character 14 indicating an unknown

I am developing an Angular 8 application. Currently, I am incorporating AlertifyJs into my project. In the styles.css file of Angular, I have imported these libraries: @import '../node_modules/alertifyjs/build/alertify.min.js'; @import '. ...

Utilizing Jquery, PHP, and Mysql to Modify Values

Looking for assistance with updating an edited value in a MySQL database using jQuery/PHP. I have three buttons: edit/save/cancel When I click on the edit button, the span data is pushed into an input text field and the edit button is replaced with the s ...

Using the variable in jQuery allows you to easily extract the filename

I am currently working on enhancing my jQuery skills by figuring out a way to extract the filename in order to display it within an element when retrieving data from an Ajax call. For example, if the string 0.2 is assigned to the variable attid, I aim to ...

Tips for creating a Nuxt.js server-side rendering (SSR) setup with partially static pre-rendered

While there is plenty of information available on Nuxt SSR and full static sites, I am struggling to find a guide on how to create a hybrid SSR with static pages combined. Currently, I am working on a website using Nuxt SSR and my goal is to statically ge ...

Tips for changing the text of a submit button with JQuery

It's strange that the code $("#thebutton").val("New text") isn't functioning as expected. ...

Manipulating arrays in JavaScript through HTML processing

I'm encountering an issue while trying to send an array to a JavaScript function. Here's what I have so far: Within the controller: @data = [{date: '2014-08-17'}, {date: '2014-08-20'}].to_json In the view: <%= content_t ...

Including external JavaScript file

Is it expected to see "Test" in a message box following the code below? If not, what modifications should be done? <html> <base href="http://www.google.com"></base> <body> <script language="JavaScript" type="text/javascript" src ...

Using an ASP.NET control along with jQuery within a standalone .js file

I recently created a jQuery voting script that functions perfectly when the code is kept within the header of the page. However, I am interested in transferring it to a separate .js file and simply including this .js file at the top of the page. Strangely, ...

Restricting users from submitting a form unless they choose a value from a select box in PHP

I am currently working on a project that requires admin privileges to add new users. Within the ADD NEW USER form, I have included a select box for Designation that is populated from the database. My goal is to display an error message if the user fails t ...

Using Struts2 actions in combination with jQuery AJAX calls

Similar Question: How to utilize $.ajax() method in struts2 In simple terms: 1. Could someone explain how to trigger a struts action using jquery ajax? (without using the struts jquery plugin) 2. How do I retrieve results and display HTML output cor ...

Send a function from a parent to its child component

I could use some assistance with JavaScript as it's not my strong suit. I'm currently trying to implement a solution from this answer on passing a function from a parent to a child in REACT, but I'm encountering some challenges. Would anyon ...

Converting CSS into jQuery

I am exploring ways to create a collapsible section with arrow icons (right and down arrows) in jQuery or JavaScript. Can anyone provide guidance on how to convert my CSS styling into jQuery code? Below is the jQuery approach I have attempted: $(document ...

Error Message: "Angular Mocks and Jasmine encountered an issue: 'fn' is not a function, instead got Object"

I'm encountering a problem when it comes to unit testing in Angular using Angular-Mocks, Jasmine, and CoffeeScript. The issue lies within this code snippet: 'use strict' describe 'sample suite', -> beforeEach inject(($ro ...

Troubleshooting: Issue with sending JSON data to front-end alongside HTML in Django

Here is the code snippet in question: import json class UserPageView(TemplateView): def get(self, request, *args, **kwargs): obj = User.objects.get(pk=17).username return TemplateResponse(request, template="user.html", context={' ...

Serve static files from a specific directory in Express.js that is not the root path

I am in need of specific files for a particular page <link rel="stylesheet" href="/vendor/css/style.css"> <script type="text/javascript" src="/vendor/js/app.js"></script> I have configured the express static route like this: express = ...

Assign a value to a Select option using JavaScript

Is there a way to populate the options in a <select> tag with values from an AJAX response array? Here is my current code: $.ajax({ type: "GET", url: "http://.......api/1/AbsenceType", dataType: "json", success: function (data) { ...

merge various useState functions for appending values to an array into a unified function in a React component

I am currently facing a challenge with code complexity. I have 8 arrays where I need to add items based on a button click. As of now, I have created 8 separate functions for each task. Is there a way to consolidate all these functions into one function tha ...

Determine the true screen dimensions of a physical device

Struggling with accurately determining a real device's screen size. Take a look at the code snippet: http://jsfiddle.net/glenswift/CpBkU/ When resizing the output frame, the h1 text changes. While I have pixel sizes for various parts of the h1 elemen ...

End the connection to mysql once several queries have been executed

I am currently working on a project that involves fetching data from an API using node.js and running multiple mysql queries in a loop to update rows. However, I am facing an issue where the script keeps running until I manually terminate the mysql connect ...

Getting an Angular error while adding data to MongoDB with Unshift

I encountered an error when submitting data to MongoDB using Angular. The error is related to the use of the unshift keyword, but I am not very familiar with Angular. Can someone help me understand why this error is occurring? TypeError: Cannot read prope ...