How can you swap out a forward slash in vue.js?

I am facing a coding issue that I need help with:

<template slot="popover">
  <img :src="'img/articles/' + item.id + '_1.jpg'">
</template>

Some of the numbers in my item.id contain slashes, leading to certain images not being displayed. I would like to either replace the slash with an underscore or remove it altogether when a slash is present in the item.id. Can someone suggest a straightforward solution for this problem?

Note that the replacement should occur only in this specific portion of code and not anywhere else where item.id is utilized.

Answer №1

To transform slashes into dashes in a computed property, you can use the replace method along with a regex pattern:

<template slot="popover">
  <img :src="`img/articles/${itemId}_1.jpg`">
</template>

<script>
...
  computed: {
    itemId: function(){
        return this.item.replace(/\//g, '-');
    }
  }
...
</script>

For demonstration, here's a test fiddle link: https://jsfiddle.net/pqfvba6n/

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

Guide to transferring data from Laravel to Vue Router?

I am currently working on a Laravel-Vue project and I need to transfer the base URL from the Laravel view to the Vue router component. For example, passing this data: {{ url('/') }}. Can anyone advise if it is achievable to pass this information ...

Dealing with HTML fields that share the same name in various positions can be tricky

Currently, I have developed a non-commercial web application using basic HTML, PHP, and Javascript along with Dynamic Drive's Tab Content code. This app serves as a tool for managing the books in my home library as well as on my ereader. My main goal ...

The fadeIn callback doesn't seem to function properly when triggered within the success function of jquery.ajax

Using AJAX, I fetch some data and prepend it to the body. Once displayed, I need to execute some client-side operations on this new element, such as rendering Latex using codecogs' script. Below is a snippet of my code: $.ajax({ /* ... */ success: fu ...

Prevent clicking on form until setInterval has been cleared using React useEffect

Here is a link to a sandbox replicating the behavior: sandbox demo I have integrated a hook in a React component to act as a countdown for answering a question. React.useEffect(() => { const timer = setInterval(() => { setTimeLeft((n ...

Trouble obtaining AJAX result using onClick event

As a newbie to AJAX, I am still trying to grasp the concept. My task involves using an AJAX call to extract specified information from an XML file. Even after carefully parsing all tag elements into my JavaScript code, I encounter a problem when attempting ...

Challenges arise when trying to display real-time Firebase data in a tabular format

I established a Firebase RealTime database with the following Values: Table: tutorial-vue-eb404 Root: other children year, author, publisher, title Using npm, I initialized my project The configuration was set up by creating firebase.js import Firebase ...

Using Next-Image Relative Paths can lead to 404 errors when being indexed by web crawlers

I have implemented next-image in my project, and all the images are hosted on Cloudinary. The images display correctly, but when I run a website analysis using tools like Screaming Frog, I receive numerous 404 errors. This is because the tools are looking ...

JavaScript: Modifying the Style of a :lang Selector

Currently, I am working on creating a basic multilingual static website using only HTML5, CSS3, and vanilla JavaScript. The structure of the HTML looks something like this: <html> <head> <title>WEBSITE</title> ...

Having issues with v-for in Vuejs? It seems to be looping multiple times

<div v-for="item in items" :key="item.id"> <div v-for="accordion in accordions" :key="accordion.title" class="line" :class="{ green: accordion.text === 'AllaboutVue', red: accordi ...

Add content or HTML to a page without changing the structure of the document

UPDATE #2 I found the solution to my question through Google Support, feel free to read my answer below. UPDATE #1 This question leans more towards SEO rather than technical aspects. I will search for an answer elsewhere and share it here once I have th ...

insert a new field into the database within Prisma while ensuring existing table data remains untouched

I have been working with the Prisma ORM and I currently have some data in my table. This is how my model looks: model User { id Int @default(autoincrement()) @id username String @db.VarChar(100) @unique } model Post { id Int @id @defa ...

The characteristics that define an object as a writable stream in nodejs

Lately, I've been delving into the world of express and mongoose with nodejs. Interestingly, I have stumbled upon some functionality that seems to work in unexpected ways. In my exploration, I noticed that when I create an aggregation query in mongoos ...

I would like to know more about the concept of getters and setters and how they can be effectively utilized

I've been struggling to understand the concept of getters and setters. Despite reading articles like JavaScript Getters and Setters and Defining Getters and Setters, I just can't seem to grasp it. Could someone please explain: The purpose of g ...

Achieve a smooth sliding effect for a div element in Vue using transition

When utilizing transitions in conjunction with v-if, it appears that the div is initially created and then the animation occurs within that div. Is there a way to have the div move along with the text during the animation? For example, when clicking on th ...

When using Vue-apollo, the queries are not functioning properly and are resulting in a 400 Bad Request error

I am currently in the process of developing a frontend using Vue cli and a backend with Django integrated with Graphene. At the moment, I am facing an issue where my mutations are working perfectly fine, but the queries seem to encounter some problems. In ...

Utilizing jQuery to update dropdown selection based on JSON object values

In my JSON value, I have the following roles: var roles = { "roles":[ {"role_id":2,"role_name":"admin"}, {"role_id":4,"role_name":"QA"}, {"role_id":3,"role_name":"TL"}, {"role_id":5,"role_name":"user"}, {"role_id":1,"role_name":"r ...

Animating progress bars using React Native

I've been working on implementing a progress bar for my react-native project that can be used in multiple instances. Here's the code I currently have: The progress bar tsx: import React, { useEffect } from 'react' import { Animated, St ...

Iterate through each entry in the database and identify and fix any duplicate records

In my form, I have a JSON array that includes the name and value of each input. I am attempting to add an "optionprix" attribute for each input with the "optionname" class based on the corresponding value of the input with the "optionprix" class. Here is ...

After ajax, trigger change event

Can anyone assist me in combining multiple functions within the same form using AJAX? The purpose of the form is to prenote a new "meeting" and it consists of an input for the date and a select dropdown for choosing the operator. FORM CODE: <div id=&qu ...

Mastering socket emission and disconnection in reactjs

When using the onchange function, I am able to open a socket and emit/fetch data successfully. However, a new socket is opened on any event. I need to find a way to emit data from the same socket ID without opening a new socket each time. Could you pleas ...