0

I'm trying to get multiple parameters of root page of my Angular 2 application. I want to get param1, param2 ... of path like "http://example.com/param1/param2/.." in my HomeComponent. For example, the path may seem like: "http://example.com/telephones/accessories/cases" But i can get only the first parameter.

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: '', loadChildren: () => import('./pages/home/home.module').then(m => m.HomeModule) },
  { path: ':a', loadChildren: () => import('./pages/home/home.module').then(m => m.HomeModule) },
  { path: ':a/:b', loadChildren: () => import('./pages/home/home.module').then(m => m.HomeModule) },
  { path: '**', redirectTo: '', pathMatch: 'full' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

home-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomeRoutingModule { }

home.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit 
{
  id1:any;
  id2:any;

  constructor(private activateRoute: ActivatedRoute,){}

  ngOnInit() {
      this.id1 = this.activateRoute.snapshot.params['a'];
      console.log("id1 - "+this.id1);
      this.id2 = this.activateRoute.snapshot.params['b'];
      console.log("id2 - "+this.id2);
    };
}

On http://localhost:4200/param1 it works, but http://localhost:4200/param1/param2 is not, i'm getting error:

GET http://localhost:4200/param1/runtime.js net::ERR_ABORTED 404 (Not Found)

I've try another way like this:

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: '', loadChildren: () => import('./pages/home/home.module').then(m => m.HomeModule) },
  { path: '**', redirectTo: '', pathMatch: 'full' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

home-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home.component';

const routes: Routes = [
  { path: '', component: HomeComponent, pathMatch: 'full' },
  { path: ':a', component: HomeComponent, pathMatch: 'full' },
  { path: ':a/:b', component: HomeComponent, pathMatch: 'full' },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomeRoutingModule { }

with absolutely the same result.

Please, advise how can i resolve my problem?

2 Answers 2

1

have you added base in your index.html?

<!doctype html>
<html lang="en">
  <head>
    <base href="/">
  </head>
  <body>
   <my-app>loading</my-app>
  </body>
</html>

in example you can download and build example it will have broken index.html

Sign up to request clarification or add additional context in comments.

3 Comments

i provide APP_BASE_HREF in my app.module.ts : providers: [{provide: APP_BASE_HREF, useValue: '/'}], but i also have try your variant with same no result
your example is working perfect, i'm trying to understand what wrong in my structure (it is some different from your)
Resume: i've used providers: [{provide: APP_BASE_HREF, useValue: '/'}], instead of <base href="/"> in index.html supposing it is same, but on the practice, in my case they do it differently. @Radik thank you!
0

use children to achieve what you want

const routes: Routes = [
  { path: '', 
   component: HomeComponent,
   pathMatch: 'full',
   children: [
      { path: ':a', 
        component: HomeComponent,
        pathMatch: 'full', 
        children: [
           { path: ':b', component: HomeComponent, pathMatch: 'full' }
        ]
      }
   ]
];

1 Comment

Katerina, unfortunately it's not working. In your descision i can't get even first parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.