Exploring the application of javascript method within a Vue template

Currently, I am attempting to extract a numeric value from the end of a URL. However, I am encountering an error in doing so. It has been a while since I last worked with Vue, but I know that we can use methods/functions to achieve the desired outcome. Could someone please point out where my mistake lies?

<ul>
    <li  v-for="(character, index) in characters" :key="index">
     <router-link :to="'/characters'+ character.url.split("/").pop()">
        {{character.name}}
      </router-link>
    </li>
  </ul>

https://i.stack.imgur.com/IrRq3.png

Answer №1

Providing an illustration to assist with understanding:

new Vue({
  el: '#app',
  data: {
    character: {
      url: 'https://jsonplaceholder.typicode.com/todos/101'
    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <div v-html="`/characters/${character.url.split('/').pop()}`"></div>
  <div> {{ `/characters/${character.url.split('/').pop()}` }}</div>
</div>

This approach utilizes string literals (known as Template literals or Template strings) in order to enhance code readability and minimize the use of quotation marks and concatenation operators like +.

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

Why does the for loop assign the last iteration of jQuery onclick to all elements?

I've encountered an issue with my code that I'd like to discuss var btns = $('.gotobtn'); $('#'+btns.get(0).id).click(function() { document.querySelector('#navigator').pushPage('directions.html', myInf ...

Utilizing Vue.js to dynamically update an Amcharts4 chart

Currently, I am utilizing AmCharts4 in conjunction with Vue.JS. Initially, I have set up the default chart design to display when the page loads. However, upon attempting to add dynamic values post-page load (via a button click), the changes do not appear ...

Improved method for importing VueRouter dynamically for named views with the use of Promises

I'm currently facing an issue with dynamically loading my component views for Vue-Router. The import statement I'm using returns a promise instead of the actual value, even when I've tried to chain ".then" in case the promise returns another ...

Accessing the outer index in a nested loop using Handlebars

Imagine I have the code below: <div> {{#each questions}} <div id="question_{{@index}}"> {{#each this.answers}} <div id="answer_{{howToGetThisIndex}}_{{@index}}"> {{this}} </div> {{/each}} </div> ...

What is the process for importing a file with an .mts extension in a CJS-first project?

Here's a snippet from a fetchin.mts file: import type { RequestInfo, RequestInit, Response } from "node-fetch"; const importDynamic = new Function("modulePath", "return import(modulePath);") export async function fetch(u ...

I'm having trouble with my Typescript file in Vscode - every time I try to edit the css code, all the text turns red. Can someone

Check out this visual representation: [1]: https://i.stack.imgur.com/9yXUJ.png Followed by the corresponding code snippet. export const GlobalStyle = createGlobalStyle` html { height: 100%; } body { background-image: url(${BGImage}); ba ...

Dividing JSON information into parts

I am attempting to present a highchart. I have utilized the following link: Highchart Demo Link Now, I am trying this web method: [WebMethod] public static string select() { SMSEntities d = new SMSEntities(); List<str ...

Ways to exchange information among Vue components?

My role does not involve JavaScript development; instead, I focus on connecting the APIs I've created to front-end code written in Vue.js by a third party. Currently, I am struggling to determine the hierarchy between parent and child elements when ac ...

Utilizing Regular Expressions in Sails.js Routing

Currently, I am working on a sails.js project and utilizing backbone for the front end. My goal is to have a single route leading to the index page where my backbone application is hosted. '/*': { view: 'home/index' } This setup ...

Tips for positioning a React component above another component

I am currently facing a challenge while working on a table with an expand more option to display additional details for each specific row. I have implemented a slider to facilitate the expansion of the table. Presently, my ExpandToggle component is embedde ...

Update the reference of the 'this' keyword after importing the file

I am currently utilizing react-table to showcase the data. My intention is to house my table columns outside of the react component due to its size and for reusability purposes. I created a table configuration file to contain all of my table configurations ...

The most secure method for retrieving User Id in AngularFire2

I'm currently facing a dilemma in determining the most secure method to obtain an authenticated user's uid using AngularFire2. There seem to be two viable approaches available, but I am uncertain about which one offers the best security measures ...

Next.js encountering page not found error due to broken link URL

Currently, I am working on implementing a login system in next.js. In the login page, I have included a Link to the Register page using the Link href attribute. However, every time I click on that link, it displays a message saying "404 page not found." Al ...

Time when the client request was initiated

When an event occurs in the client browser, it triggers a log request to the server. My goal is to obtain the most accurate timestamp for the event. However, we've encountered issues with relying on Javascript as some browsers provide inaccurate times ...

Create a new class in the body tag using Javascript

If the operating system is MAC, I set a variable and then based on a condition, I want to add a new class in the body tag. Check out my code snippet: <script type="text/javascript" language="javascript"> var mac = 0; if(navigator.userAgent.index ...

How to troubleshoot the Uncaught TypeError in Vue.js: data.filter is not recognized as a function in my code

My JSON structure looks like this: items: {"countcats":2,"countsubcats":7, "catsubcatsdata":{ "15978738e6cd1e":{"title":"Test 1","description":"blablabla", "subcats":{ "1597873b16 ...

Struggling with setting up a search bar for infinite scrolling content

After dedicating a significant amount of time to solving the puzzle of integrating infinite scroll with a search bar in Angular, I encountered an issue. I am currently using Angular 9 and ngx-infinite-scroll for achieving infinity scrolling functionality. ...

What is the significance of the appearance of the letters A and J in the console for Objects?

After running console.log() in JavaScript code, you may notice some random letters like A and j before or after the Object description in the Google Chrome browser console. What is the significance of these letters? ...

Unable to differentiate between .jsx and .js files

Here is the content of my JavaScript file: var React = require('react'); export default class AmortizationChart extends React.Component { render() { var items = this.props.data.map(function (year, index) { ret ...

What is the process for adding parameters to a Fetch GET request?

I have developed a basic Flask jsonify function that returns a JSON Object, although I am not certain if it qualifies as an API. @app.route('/searchData/<int:id>',methods=["GET"]) def searchData(id): return jsonify(searchData(id)) Curr ...