Unable to transmit props through components with Vue router

Hey there, I'm currently facing an issue with passing props from my vue router. It seems like nothing is being printed and when I checked in the mounted hook, it's returning undefined. However, strangely enough, when I use

console.log(this.$route.params.id);
I do get a value. But when I try to access it with this.id, it returns undefined. In my User template, no output is being displayed. The same code works perfectly fine in the online tutorial that I'm following. Can anyone help me out with this? Could there have been some modifications in a recent release?

let User = {
  props: ['id'],
  template: `
            <div>Hello # {{id}}</div>
        `,
mounted() {
    console.log(this.$route.params); // this returns the value
    console.log(this.id); // this gives undefined
  }
}

let App = {
  template: `
            <div class="wrapper">
                <router-view></router-view>    
            </div>
        `
}

let router = new VueRouter({
  routes: [{
    path: '/user/:id',
    component: User,
    props: true
  }, ],
})

let app = new Vue({
  el: '#app',
  router: router,
  components: {
    'app': App
  }
})

router.push('/user/1')
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1761627257253921392623">[email protected]</a>/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c7a7969217e637978697e4c3e223c223c">[email protected]</a>/dist/vue-router.js"></script>

<div id="app">
  <app></app>
</div>

Answer №1

Your Vue Router version is outdated. Update to the latest version - 3.5.2 - to ensure your code functions properly....

let User = {
  props: ['id'],
  template: `
            <div>Hello # {{id}}</div>
        `,
  mounted() {
    console.log(this.$route.params); // successfully returns the value
    console.log(this.id); // currently undefined
  }
}

let App = {
  template: `
            <div class="wrapper">
                <router-view></router-view>    
            </div>
        `
}

let router = new VueRouter({
  routes: [{
    path: '/user/:id',
    component: User,
    props: true
  }, ],
})

let app = new Vue({
  el: '#app',
  router: router,
  components: {
    'app': App
  }
})

router.push('/user/1')
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dea8abbb9eecf0e8f0efea">[email protected]</a>/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d3d0c088d7cad0d1c0d7e5968b908b97">[email protected]</a>/dist/vue-router.js"></script>

<div id="app">
  <app></app>
</div>

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

What is the best way to display user input within a paragraph using React?

I've been working on a To-Do list and I have successfully implemented the functionality to add new tasks. The issue I'm facing is that when I try to add a task, it doesn't show up in the paragraph within my Todo component, even though I can ...

Choose2 incorporate on change

I have a Laravel project where I am using the vuexy theme. I've been trying to add an onchange event to my select2 input, but so far I haven't had any success. Here is my select2 input: <div class="col-12 mb-2"> <label class ...

I am having trouble retrieving a JsonResult from an asp.net mvc controller using $resource in angular

I am new to Angularjs and trying to integrate it with asp.net mvc. I am facing an issue where I am unable to access an asp.net mvc controller to return a JsonResult using $resource in angular. Strangely, when I use $.getJson in JavaScript directly, it work ...

No data returned from API call in Next.js and Strapi

Recently, I encountered an issue with my Next.js frontend application that fetches data from a Strapi backend. Despite seeing requests being made in the Strapi developer logs, the retrieved data is empty. Below is a snippet of my Next.js code: import { us ...

Error message: Cypress Vue component test fails due to the inability to import the Ref type (export named 'Ref' is missing)

Recently, I created a Cypress component test for my Vue component by mounting it and verifying its existence. The component utilizes Vue's Ref type and is structured as a TypeScript component. However, during the test execution, Cypress encountered a ...

Creating a three.js visualization of a cheerful X-Y coordinate graph

I am looking to generate a positive X-Y plane in mesh format using three.js. Additionally, I would like the ability to click on any intersection point and retrieve the coordinate values, resembling a graph paper layout. Design.prototype.mouseUp = functi ...

Every time an npm installation is attempted, the following error occurs: "npm ERR! Cannot read property 'resolve' of undefined."

Welcome Everyone! Currently, I am facing an issue on my dual boot system where Node and NPM were functioning smoothly on Windows 7. However, now that Windows 7 is not booting up, presumably due to hardware problems, I have resorted to using Windows 10. E ...

Encountering an issue with JQuery when attempting to create a double dropdown SelectList. Upon submitting the POST request, the output received is always

Two dropdownlists have been created, where one acts as a filter for the other. When selecting a customer from the dropdown Customer, only a limited set of ClientUsers is displayed in the dropdown ClientUser. This functionality is achieved using a jQuery fu ...

Tips for repairing buttons in the CSS container

The buttons in the CSS search-box are not staying fixed as intended. They should be within the search box, but instead, they are protruding out of the panel. I attempted to utilize z-index, but it did not produce the desired outcome. https://i.sstatic.n ...

Looking to showcase the outcome of the Procedure invocation when I made the call?

{ "isSuccessful": true, "resultSet": [ { "name": "pradeep", "password": 123, "timestamp": "2014-04-08T12:58:45.000Z" }, { "name": "dileep", "password": 1234, "timestamp": "2014-04-08T13:00:52.000Z" } ] } I have ...

Tips for making a table have the same width as its child td elements and enabling the table to exceed the width of the viewport

I am facing a challenge with a table that has dynamically changing rows and columns. <table> <tr> <td>column 1</td> <td>column 2</td> <td>column 3</td> </tr> <tr> <td> ...

Generate a dynamic file import loop within a React application

Suppose I have a directory containing several .md files: /static/blog/ example_title.md another_example.md three_examples.md and an array that includes all the titles: const blogEntries = ["example_title", "another_example", "three_examples"] In ...

"Using Vue.js to create a Single Page Application with Facebook

Creating my own social network using Node.JS, Hapi, and Vue. Looking to incorporate Facebook login. Since Facebook Auth can't be implemented in a Single Page Application (SPA), I have devised the following approach. Integrate a login button on the S ...

"Adding an Image within a Textbox Using Ext JS: A Step-by-Step

How can I add a search image inside a textbox created with the following code? I have created a table with a footer, and within one of the textboxes in the table, I want to include a search button that will call another function when clicked. However, desp ...

What is the best way to invoke a function using a string as its name?

In my grid configuration function, I am assigning column definitions. One key, valueGetter, requires a function to be called to fetch the column value. The issue I am encountering is that the API returns this value as a string. When I try to set it using ...

Unable to modify app.message to 'something' in the console

While experimenting with vue.js at https://v2.vuejs.org/v2/guide/, I added the file to my view. The First App example successfully displays "Hello Vue!" when the app loads. However, upon trying to change app.message from the console, the title does not up ...

Tips on customizing the appearance of the dropdown calendar for the ngx-daterangepicker-material package

Is there a way to customize the top, left, and width styling of the calendar? I'm struggling to find the right approach. I've been working with this date range picker. Despite trying to add classes and styles, I can't seem to update the app ...

javascript detect when two div elements are overlapping

On my webpage, I have implemented the effect.shrink() function. However, when clicking quickly on the page, the div tags start overlapping with other elements. What is the best way to solve this issue? I am using both scriptaculous.js and prototype.js fo ...

When a button is clicked, load two separate pages into two distinct divs at the same time

$('#menuhome').click(function(){ $('#leftcolumncontainer').load('pages/homemenu.php'); }); the code above is used to load the home menu on the left side. Now, let's add the following: $('#menu ...

Unable to locate the internal/fs module, current solutions are proving ineffective

Every time I try to run my project, I encounter the same issue despite downgrading my node version to 8.4.0 (npm version 5.3.0). I have tried various solutions such as removing the node_modules, running npm cache clean --force, and then npm install, but no ...