What is the procedure for sending a secondary route parameter to the angular 2 services module?

I'm a beginner with Angular 2 and I'm seeking advice on how to pass multiple parameters to Angular's service module. For instance, here is the first parameter I'm passing to the service through the router link.

In the home-component.html file:

<a href="#" [routerLink]="['/product', product.slug]">

This link is connected to the route defined in app.routes.ts which loads the ProductOverviewComponent.

const appRoutes: Routes = [
{ path: 'product/:slug', component: ProductOverviewComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: '', component: HomeComponent, canActivate: [AuthGuard] },

// Redirect to home if no matching route
{ path: '**', redirectTo: '' }
];

The product-overview.component.ts file utilizes the getProductOverview() method.

ngOnInit() {
  this.sub = this.route.params.subscribe(params => {
    let slug = params['slug'];
    console.log('getting product with slug: ', slug);
    this.productoverviewservice.getProductOverview(slug)
    .subscribe(p => this.product = p);
  });
}

This method then calls the getProductOverview function from product-overview.service.ts

getProductOverview(slug: string): Observable<Course> {
    let product$ = this.http
        .get(`${this.baseUrl}${slug}/` , options)
        .map((response: Response) => response.json());
}

My inquiry revolves around how to include a second route parameter in the getProductDetail function?

For example:

getProductDetail(slug: string): Observable<Course> {
    let productDetail$ = this.http
        .get(`${this.baseUrl}${slug}/${slug2}` , options)
        .map((response: Response) => response.json());
}

Any tips or suggestions are greatly welcomed.

Answer №1

Make sure your routing configuration looks like this:

{path: 'product/:id/:name', component: ProductDetailsComponent, canActivate: [AuthGuard] }])

With these changes, everything should work perfectly!

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

Uncertainty surrounding refinement in Typescript

I've been diving into the book Programming TypeScript, and I'm currently stuck on understanding the concept of refinement as shown in this example: type UserTextEvent = { value: string; target: HTMLInputElement }; type UserMouseEvent = { value: [ ...

How to stop a form from being cleared when a button is clicked in Angular 2

Within my form, there is a button that allows users to add an item to the object's array. <form (ngSubmit)="submit()" #myForm="myForm" class="form-horizontal" style="direction: ltr"> <div class="row"> <div class="col-md-6"> ...

Clearing the Angular cache is necessary every time new changes are added to production mode

Currently, I am working with angular 9.0.3 and after making some new changes, I upload it to Cpanel and compile using the following command: ng build --prod --aot --outputHashing=all --extractCss=true. However, once uploaded, if I visit the website and en ...

Generating interactive forms in Angular 7 using API data

I have developed a component that generates dynamic forms using angular reactive forms. When I attempt to utilize this component to create form fields in another component based on the response from my API, the form appears disabled and I am unable to make ...

Angular form retains the previous value when saving

I encountered an issue with my form component where it displays previous values instead of updated ones during a save operation. The strange part is that if I close the form and reopen it, the new values are then shown correctly. It seems like the problem ...

Font Awesome functions properly on one Angular component but not on the second one

I've encountered a strange issue while working on my Angular project. About a month ago, I added a component with a form that included some inputs along with Font Awesome icons. The code snippet is provided below. <div class="field"> ...

What is the best way to retrieve an item from an array?

I have an array containing multiple records and I need to extract just one record from it using its id as an object. However, the result I'm getting is an array with that single record. Is there a way to resolve this issue? Result: [{…}] 0: {id ...

Stop extra properties from being added to the return type of a callback function in TypeScript

Imagine having an interface called Foo and a function named bar that accepts a callback returning a Foo. interface Foo { foo: string; } function bar(callback: () => Foo): Foo { return callback(); } Upon calling this function, if additional pr ...

Display the dynamic change of minutes and seconds every second on an Ionic HTML page

I created a JavaScript countdown counter that displays minutes and seconds using the following line of code: document.getElementById("demo").innerHTML = Number(this.minutes) + 'm' + Number(this.seconds) + 's '; However, on iOS devices ...

Saving the contents of a JSON query field to a file using Angular

Currently, I am working on an Angular application that interacts with a JSON API to extract specific fields from the data and save them to a file. For instance, if the API query yields: [{"id":1,"name":"Banana"},{"id" ...

Adjust the component suppliers based on the @input

If I were to implement a material datepicker with a selection strategy, I would refer to this example There are instances where the selection strategy should not be used. The challenge lies in setting the selection strategy conditionally when it is insta ...

The installed NPM package does not contain the necessary TypeScript compiled JS files and declaration files

I have recently released a TypeScript library on NPM. The GitHub repository's dist (Link to Repository Folder) directory includes all compiled JavaScript and d.ts files. However, after running npm i <my_package>, the resulting module contains on ...

Configuration file for proxying both HTTP requests and WebSockets to the same endpoint

In my development environment, I have successfully set up a proxy for http requests to a django server on my backend/laptop using a proxy.conf.json configuration file: { "/graphql": { "target": "https://localhost:443/gr ...

"Using Angular and TypeScript to dynamically show or hide tabs based on the selected language on a website

When switching the language on the website, I want to display or hide a specific tab. If the language is set to German, then show the tab; if any other language is selected, hide it. Here's my code: ngOnInit(): void { this.translate.onLangChange.s ...

What is the best way to bring a module into a dialog in Angular (8)?

I've developed a reusable dialog component for my Angular project, but I'm facing an issue when trying to import modules inside the dialog. Strangely, the page stops functioning properly. Here's the hierarchy of my project: https://i.sstat ...

How can I limit the input of string values from a Node Express request query?

export type TodoRequest = { order?: 'asc' | 'desc' | undefined; } export const parseTodoRequest = (requestData: ParsedQs): TodoRequest => { return { order: requestData.order as 'asc' | 'desc' | u ...

Is there a way to show a string value stored in Java onto an Angular 8 display?

Hey there! I am just starting out with angular 8 and have a java project where I'm using JDBC to connect to my beloved MySQL database. I have some valuable data stored in a MySQL table that I access in java and store in strings (or even a list). Now, ...

Error: Typescript error at line 18 in app.ts - Cannot locate the 'server' namespace

Check out this straightforward code snippet: "use strict"; import * as express from "express"; class Server { public app: express.Application; public static start(): Server { return new Server(); } constructor() { this. ...

PrimeNG Component Containing a Dynamic Dialog Instance

I am experiencing an issue with handling dynamic dialogs in PrimeNG. Is there a solution for managing actions on a dialog other than just using the close option? For instance, in the context of the Kendo-UI dialog example, I can specify the content.insta ...

An issue has been identified with the functionality of the router-out

Issue with Router Loading Component Outside of the router-outlet in app.component.ts @Component({ selector : "body", template : `<router-outlet></router-outlet>`, directives: [ROUTER_DIRECTIVES] }) @RouteConfig([ {path: "/aut ...