Angular 2 在路由中传递参数

Angular 2 pass parameters in Routes

是否可以像此处示例那样在路由中传递参数 - 参数:['parameters']

我不是说在 Url 中传递数据,但是当我在 pageone 上时,我可以访问此视图的路由中的参数。

import { RouterModule, Routes } from "@angular/router";
import { HomeComponent } from "../app/home.component";
import { PageOneComponent } from "../app/pageone.component";
import { AppComponent } from "../app/app.component";

const mainRoutes: Routes = [
    {
        path: "",
        component: AppComponent,
        resolve: {
            app: AppResolverService
        },
        children: [
            {
                path: "",
                component: HomeComponent
            },
            {
                path: "/pageone",
                component: PageOneComponent
                params: ['parameters']
            },
        ]
    }
];

export const MAIN_ROUTE_MODULE = RouterModule.forRoot(mainRoutes);

您需要使用其他方式传递参数。

检查这个:Router configuration

The data property in the third route is a place to store arbitrary data associated with this specific route. The data property is accessible within each activated route. Use it to store items such as page titles, breadcrumb text, and other read-only, static data. You'll use the resolve guard to retrieve dynamic data later in the guide.

还有这个:Routes type

如果你想在url中有数据,你可以试试这个:https://angular.io/guide/router#heroes-list-optionally-selecting-a-hero

它有点笨拙,但您可以使用 UrlMatcher 来设置任意参数。就像这个例子:

const mainRoutes: Routes = [
  {
    // ...
    children: [
      {path: "", component: HomeComponent},
      {
        path: "/pageone",
        component: PageOneComponent,
        matcher: (url: UrlSegment[]) => ({
          consumed: url,
          posParams: {pageName: new UrlSegment('pageone', {pageName: 'pageone'})},
        }),
      }
    ]
    // ...
  }];