Issues have been reported regarding the paramMap item consistently returning null when working with Angular 8 routing

I am encountering an issue with Angular 8 where I am trying to fetch some parameters or data from the route but consistently getting empty values. The component resides within a lazy-loaded module called 'message'.

app-routing.module.ts:

...

{
  path: 'message',
  loadChildren: () =>
    import('./systems/message/message.module').then(
      m => m.MessageModule
    )
},

...

message-routing.module.ts:

...

const routes: Routes = [
  {
    path: '',
    component: MessageIndexComponent,
    children: [{ path: ':folder', component: ComponentMessageListComponent }]
  },
  { path: 'add', component: MessageAddComponent },
  { path: 'list', component: MessageIndexComponent }
];

...

component-message-list.component.ts:

...

constructor(
  private route: ActivatedRoute
) {}

ngOnInit() {
  console.log(this.route.snapshot.paramMap.get('folder'));
   // Change list on date change
  this.route.paramMap.subscribe(params => {
    console.log(params.get('folder'));
  });
}

...

When navigating to this address:

/message/inbox

UPDATE:

I have tried navigating to the route by directly typing the address in the browser:

or using an anchor tag:

message-index.component.html

...

  <a class="list-group-item list-group-item-action d-flex p-3" routerLink="inbox" routerLinkActive="active">
    Inbox
  </a>
...

The parameter always appears null in both scenarios.

Answer №1

Instead of accessing the parent params, I needed to retrieve the firstChild params. The previous code was only returning the parameters of the parent element.

The following code is now functioning correctly:

this.route.firstChild.paramMap.subscribe(params => {
    console.log(params.get('folder'));
  });

UPDATE: I discovered that the issue stemmed from forgetting to replace the child component tag in the parent with 'router-outlet'. As a result, the child component was being inserted into the parent without utilizing the router functionality!

Thank you to everyone who assisted me in resolving this matter.

Answer №2

If you're looking for a solution, consider the following approach:

Utilize the code snippet below to achieve desired results:
this.router.parseUrl(this.router.url).queryParams[key]

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

Issue with Angular project: View not being updated when using behaviorSubjects

Within my Angular project, I am retrieving data from an API using a service and storing the data within a BehaviorSubject as shown below private categorySubject = new BehaviorSubject<number | null>(null); apiBehavior = new ReplaySubject<ApiRes ...

Retrieving the Selector Value during a Change Event

Is there a way to retrieve the selector value in a change event? I attempted this approach: $("#frek_menonton_tv :input").change(function(){ $(this).selector; }); However, it only returns an empty string. Desired outcome: frek_menonton ...

Conceal the toolbar element if it is not at the top of the page

I am encountering an issue with my toolbar. When I scroll down, the transparent background of the toolbar disappears and it looks like this: . How can I hide this component when the page is scrolled down and show it again when scrolling back up to the top ...

Are foreign characters the culprit behind the JSON parsing problem?

I am encountering an issue while attempting to parse a field containing JSON data returned from an API. The data includes various unusual characters like East Asian symbols and curly quotes, causing this error to appear. I am unsure of how to resolve it - ...

What is the method for implementing type notation with `React.useState`?

Currently working with React 16.8.3 and hooks, I am trying to implement React.useState type Mode = 'confirm' | 'deny' type Option = Number | null const [mode, setMode] = React.useState('confirm') const [option, setOption] ...

Monitoring Website Load Speed using Performance API

After attending a recent talk by Steve Souders, I was fascinated by the discussion of the new performance spec being implemented by modern browsers. During his presentation, he used an example to demonstrate how to measure perceived page load time: var ti ...

Utilize Bootstrap tooltips to display live results fetched via an AJAX call

I have a bootstrap tooltip that I am trying to populate with data from an AJAX request. The text retrieved from the request should be displayed as the title property of the tooltip. Despite successfully executing the AJAX request, I am facing two issues: ...

Remove the post by utilizing the $.ajax function

I am just starting out with using $.ajax and I'm not very familiar with it. I have a button that is meant to delete a user post based on the article ID provided. <button type="button" onclick="submitdata();">Delete</button> When this but ...

Utilize Vue.js to take screenshots on your device

After following the tutorial at https://www.digitalocean.com/community/tutorials/vuejs-screenshot-ui, I was able to successfully capture a screenshot with Vue.js. However, it seems that the dimensions of the screenshot are not quite right. Issue: The cap ...

Angular fails to identify modifications in specific project files

https://i.stack.imgur.com/9XRnS.png After making changes to auth.gaurd.ts, create-order.service.ts, and order.model.ts, I'm facing an issue where the changes are not being reflected in my application. Interestingly, all other files seem to work fine. ...

AngularJS approach to binding window scroll

Hey there, I'm a newcomer to AngularJS with a background in jQuery. I'm trying to create a fixed-top navbar using AngularJS that has a background which changes from transparent to opaque as the window is scrolled. Unfortunately, I'm struggli ...

When the browser's back button is clicked, no action occurs with Next/router

I am confused about why my page does not reload when I use the browser's back button. On my website, I have a catalog component located inside /pages/index.js as the home page. There is also a dynamic route that allows users to navigate to specific p ...

Adding a navigation bar to every administrator page while excluding it from shop pages can be achieved by creating a

I am facing a challenge in implementing a navbar and sidebar on all admin pages, except for the shop page. The issue arises because I have set my sidebar and navbar to be global. My goal is to make them global only for admin pages and not for the shop. He ...

JavaScript - Modifying repeating numbers in an array

I have a list of numbers that repeats multiple times like this; numbers = [2,3,1,2,1,3,3,1,2] My goal is to increase each repeated number by 10 every time it appears. Therefore, the updated list should look like this; updated_numbers = [2,3,1,12,11,13,23, ...

What is the datatype for an Angular FormControl when dealing with numbers?

Having an issue with Angular FormControl when using it for text and number inputs. Both inputs are being recorded as text even though one is supposed to be a number. var control = this._formBuilder.control(this.settings[input.property]); control.valueChan ...

Establishing foreignObject coordinates in Angular

Struggling with setting the position (x, y) for foreignObject in Angular. I have attempted the following: <foreignObject width="65" height="50" x="{{position?.x}}" y="{{position?.y}}"> <div class="c ...

Setting up popover functionality in TypeScript with Bootstrap 4

Seeking assistance with initializing popovers using TypeScript. I am attempting to initialize each element with the data-toggle="popover" attribute found on the page using querySelectorAll(). Here is an example of what I have tried so far: export class P ...

Exploring the depths of complex objects with the inclusion of optional parameters

I've been working on a custom object mapping function, but I've encountered an issue. I'm trying to preserve optional parameters as well. export declare type DeepMap<Values, T> = { [K in keyof Values]: Values[K] extends an ...

Create a TypeScript function called toSafeArray that transforms any input into a properly formatted array

Is there a way to create a function that can transform any type into a safe array? toArray(null) // []: [] toArray(undefined) // []: [] toArray([]) // []: [] toArray({}) // [{}]: {}[] toArray(0) // [0]: number[] toArray('') // ['']: str ...

Dealing with callback errors: error handling is anticipated

In my Vue web application, I have enabled ESLint and encountered an issue with the following code: myApi.get('products/12').then((prodResponse) => { state.commit('ADD_PRODUCT', {product: prodResponse.data}) }, error => { cons ...