If I include beforeRouteEnter in one component, the this.$route property may become undefined in another component

I seem to be encountering an issue. After implementing a beforeRouteEnter method in one component, I am unable to access this.$route in another component.

Here is the structure of my app:

<div id="app">
    <settings-modal></settings-modal>

    <router-view name="CalendarMonthView"></router-view>
</div>

The CalendarMonthView component is rendered in the router view and contains a beforeRouteEnter method. However, once this method is added, the SettingsModal component reports that this.$params is undefined during its created() lifecycle hook. This was not the case before implementing beforeRouteEnter.

CalendarMonthView component

export default {
    ...

    // to.params.selectedYear  = 2019
    // to.params.selectedMonth = 3

    beforeRouteEnter (to, from, next)
    {
        Month.getDays({
            year: to.params.selectedYear,
            month: to.params.selectedMonth,
         }).then(response => {
             store.monthDays = response.data
             next()
         })
    }
}

SettingsModal component

export default {
    ...

    created()
    {
        // undefined
        console.log('selectedYear: ', this.$route.params.selectedYear)

        // undefined
        console.log('selectedMonth: ', this.$route.params.selectedMonth)
    }
}

Answer №1

It seems like you're referencing the Month.getDays() function, but I'm not familiar with it. It's possible there was a mistake in the code that prevented the next() function from being triggered. I recommend verifying the accuracy of your function to ensure everything is working properly.

Answer №2

The route parameters are not yet accessible because the navigation process has not been completed. It is necessary to wait for the promise returned by Month.getDays to be resolved.

Attempting to retrieve the parameters from SettingsModal immediately upon creation, before the promise is resolved, will result in an error.

One approach is to use a watch on $route to monitor and access the params once they are available.

watch: {
  '$route': 'checkParams'
 },

This code should be added to SettingsModal.

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 transform an array of arrays into an array of objects using AngularJS?

Here is some code I am working on: $scope.students=[]; $scope.students[[object Object][object Object]] [0]{"id":"101","name":"one","marks":"67"} [1]{"id":"102","name":"two","marks":"89"} I would like to reformat it into the ...

Recording JavaScript Cookie Visit Counts and Tracking Last Login Dates

I am a beginner in JavaScript and cookies, and I am attempting to create a cookie that can show the number of times someone has visited a website, the date of their last visit, and the expiration date of the cookie. Initially, I tried modifying code from ...

Is it possible to assign numerical values to attributes in HTML code?

I'm unsure about something - is it possible to set an attribute value as a number? For example: <div data-check="1"></div> Is this the correct way to do it or not? I've heard conflicting opinions, with some people saying you shouldn ...

What is the best way to center a heading element within a section on a webpage?

I need assistance aligning the h1 element with the specific class wish to the center of its section, both horizontally and vertically. Unfortunately, I have been unable to achieve this thus far. Any guidance would be greatly appreciated. Below you will fi ...

The iframe came to a halt as soon as it was no

I am currently developing a live video website that utilizes third-party tools to play the videos. To simplify things, I have embedded all the components required for live video into a single HTML page. Here is how it looks: <iframe data-v-140cfad2= ...

Incorporating React's dynamic DIV layout algorithm to generate a nested box view design

My goal is to showcase a data model representation on a webpage, depicting objects, attributes, and child objects in a parent-child hierarchy. I had the idea of developing a versatile React component that can store a single data object while also allowing ...

The Azure SpeechSynthesizer encountered an error due to using HTTP instead of the expected HTTPS protocol. This issue can be resolved by installing the NPM package `microsoft

Recently, I have been encountering an issue with the microsoft-congnitiveservices-speech-sdk npm package. Everything was working smoothly until randomly an error started popping up. This could be due to a browser update or a minor package update - I'm ...

Tips for sending a variable to control a particular panel within an accordion in Angular 2

I have a collection of ngbpanels that are dynamically created using ngFor. I need to expand or collapse a specific panel based on certain conditions, but the ID I provide for the panel is stored in a variable. The code does not recognize the panel ID when ...

AngularJS: Compile a particular template

One pre tag on the page contains dynamic text that is unknown during page load. This text may include ng commands, as shown below: <pre> Hello <span ng-click="test('args')">world</span> angular-JS! </pre> Since these ...

Retrieve the highlighted portion of a selected option within a Vue select element

Currently, I am attempting to retrieve the selected text (not value) of a select element using Vue: var App = window.App = new Vue({ el: '#app', data: { style: '5' }, computed: { calctitle: function() { return thi ...

Please explain this ES6 syntax to me: Using a colon after a function call

While exploring the documentation for a flux store in React, I came across an example that caught my attention. import {ReduceStore} from 'flux/utils'; class CounterStore extends ReduceStore<number> { getInitialState(): number { ret ...

How to toggle classes on specific items generated with Vue JS's v-for directive

I have a list rendering using the v-for directive in Vue.js. <li v-for="group in groupList" :key="group.id" @dragenter="toggleClass ...."@dragleave="toggleClass ...." > Content </li> My goal is to apply a class to the li el ...

`Where are functions housed in Vue when dealing with slots?`

When component A acts as the parent to component B, and B has a designated slot for content insertion, the question arises about the placement of a clickHandler. Should it be placed in A or in B? Is it possible to have both options implemented as long as ...

In Next.js, encountering an error when using canvas drawImage

I am attempting to upload a local image onto a canvas element, but I keep encountering an error: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement ...

Struggling to filter an Array within an Array based on dates falling between a specific date range

Currently, I am using a filtering method that is working perfectly, but I am facing an issue where I lose my original Array when there are no dates between the specified range (as expected). Is there a way to prevent this data loss? The reason I need to r ...

TypeORM ensures that sensitive information, such as passwords, is never returned from the database when retrieving a user

I developed a REST API using NestJs and TypeORM, focusing on my user entity: @Entity('User') export class User extends BaseEntity { @PrimaryGeneratedColumn() public id: number; @Column({ unique: true }) public username: string; publi ...

Can one button be clicked to trigger the activation of another button through a click?

Just as the title suggests, I am wondering how to make this happen? Any guidance would be greatly appreciated! $('.notVisible').click (function(){ alert("I'm the invisible button") }); $('.visible').click (function(){ al ...

Caution: React is unable to identify the `PaperComponent` prop on a DOM element

Trying to create a draggable modal using Material UI's 'Modal' component. I want users to be able to move the modal around by dragging it, so I decided to use 'Draggable' from react-draggable library. However, I encountered this er ...

Utilize datetime values in chartjs ajax for data visualization

I'm currently working on creating a chart using chart.js. I have php code that retrieves datetime data through an ajax call. However, the chart is not displaying any data and there are no visible errors. I've checked the php code using console.lo ...

What is the process of converting TypeScript to JavaScript in Angular 2?

Currently diving into the world of Angular 2 with TypeScript, finding it incredibly intriguing yet also a bit perplexing. The challenge lies in grasping how the code we write in TypeScript translates to ECMAScript when executed. I've come across ment ...