Angular2 Query Parameter from Index URL

I am trying to get request parameters from a url in a component in Angular2

Version: "angular2": "npm: angular2 @ 2.0.0-beta.12",

I am trying to extract id request parameter to component and show it

Here is the request.

local: 8080 / index.html ID = 1

boot.ts

import 'reflect-metadata';
import 'angular2/bundles/angular2-polyfills';
import { bootstrap } from 'angular2/platform/browser';
import { provide} from 'angular2/core';
import { AppComponent } from './app.component';
import {ROUTER_PROVIDERS, Location, LocationStrategy,     HashLocationStrategy} from "angular2/router";
import {HTTP_PROVIDERS} from "angular2/http";

bootstrap(AppComponent , [ROUTER_PROVIDERS, HTTP_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy})]);

Here is app.component.ts

import { Component } from 'angular2/core';
 import {Router, Location, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouteParams} from 'angular2/router';

@Component({
    selector: 'my-app',
    template: '{{welcome}} {{queryParam}}',
    directives: [ROUTER_DIRECTIVES]
})
export class AppComponent {
    welcome: string = 'Query Param test!'
    queryParam : string;

    constructor(private _location: Location, private _routeParams:     RouteParams){
        console.log(_routeParams.get('id'));
        this.queryParam = _routeParams.get('id');
    }

}

I keep getting this error:

error message

I got this solution from another post. Not sure how to fix the error.

AND

+2
source share
2 answers

RouteParamsit is not available in the root component. It is available only for routable components.

See RouterOutletsuba code, in activate , you can see the following

var providers = Injector.resolve([
  provide(RouteData, {useValue: nextInstruction.routeData}),
  provide(RouteParams, {useValue: new RouteParams(nextInstruction.params)}),
  provide(routerMod.Router, {useValue: childRouter})
]);

RouteParams RouterOutlet , ( ).

0

_location.path() AppComponent. . .

, - .

0

Source: https://habr.com/ru/post/1666339/


All Articles