【问题标题】:Angular 2: Route generator for '...' was not included in parameters passedAngular 2:“...”的路由生成器未包含在传递的参数中
【发布时间】:2015-12-26 18:34:54
【问题描述】:

所以我的 AuthenticationService 中有以下代码。检查登录凭据后,用户会被重定向到他们的个人资料:

authentication.service.ts:

redirectUser(username:string):void {
  // Redirect user to profile on successful login
  this.router.navigate(['../Profile/Feed', {username: username}]);
}

这一切都很好,但是自从我在ProfileComponent 中引入了一些子路由后,我遇到了一些错误。 首先,这是我的带有 RouteConfig 的 AppComponent:

app.ts:

import {Component, ViewEncapsulation} from 'angular2/core';
import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';

import {HomeComponent} from '../home/home';

import {AuthenticationService} from '../../services/authentication.service';
import {LoginComponent} from '../login/login';
import {ProfileComponent} from '../profile/profile';
import {ProfileService} from '../../services/profile.service';

@Component({
  selector:      'app',
  viewProviders: [AuthenticationService, ProfileService, HTTP_PROVIDERS],
  encapsulation: ViewEncapsulation.None,
  directives:    [
    ROUTER_DIRECTIVES
  ],
  templateUrl:   './components/app/app.html'
})

@RouteConfig([
  {path: '/', component: HomeComponent, as: 'Home'},
  {path: '/inloggen', component: LoginComponent, as: 'Login'},
  {path: '/profile/:username/...', component: ProfileComponent, as: 'Profile'}
])

export class AppComponent {
  ...
}

如您所见,ProfileComponent 内部定义了一些新路由。如图所示:

ProfileComponent.ts:

import {Component, OnInit} from 'angular2/core';
import {RouteConfig,ROUTER_DIRECTIVES, RouteParams, RouterLink, RouterOutlet} from 'angular2/router';
import {ProfileService} from '../../services/profile.service';
import {PROFILE_IMAGES} from '../../constants/constants';
import {WorkoutsComponent} from '../workouts/workouts';
import {FeedComponent} from '../feed/feed';

interface IProfileVM {
  username: string;
  profileUser: Object;
  getProfileData(username:string): void;
}

@Component({
  selector:    'profile',
  templateUrl: './components/profile/profile.html',
  directives:  [RouterOutlet, RouterLink],
  providers:   [ProfileService]
})

@RouteConfig([
  {path: '/nieuwsfeed', component: FeedComponent, as: 'Feed'},
  {path: '/workouts', component: WorkoutsComponent, as: 'Workouts'}
])

export class ProfileComponent implements OnInit, IProfileVM {

  public username:string;
  public profileUser:any;

  constructor(private _profileService:ProfileService,
              private _params:RouteParams) {
    this.username = this._params.get('username');
  }

  ngOnInit() {
    this.getProfileData(this.username);
  }

  getProfileData(username) {
    // Do stuff..
  }
}

所以这里的问题是,在我在 ProfileComponent 中引入一些子路由之前,一切正常。用户被重定向,用户名出现在 URL 中,一切都很好。但是由于我添加了这些子路由,我得到了以下错误:

"Route generator for 'username' was not included in parameters passed."

如果有人可以帮助我,那就太好了!提前致谢!

【问题讨论】:

  • 试试this.router.navigate(['../Profile' , {username: username}, 'Feed']);
  • 真的吗?刚刚做到了.. :-) 非常感谢!!
  • 是的,当您有../Profile/Feed, {username: username} 时,您将参数传递给Feed 而不是Profile。很高兴它起作用了;)
  • 使用 Angular v1 已经一年了,到目前为止,Angular v2 一直很痛苦 ;-)。感谢您的帮助!

标签: angular angular2-routing


【解决方案1】:

我认为您最好在路由定义中将嵌套的配置文件组件上移一个级别。任意值username 对配置文件组件路由没有影响。此外,该值实际上在子路由中是必需的,应该在此处定义。

所以父组件路由会从配置文件路由中丢失:username,看起来像这样:

@RouteConfig([
  {path: '/', component: HomeComponent, as: 'Home'},
  {path: '/inloggen', component: LoginComponent, as: 'Login'},
  {path: '/profile/...', component: ProfileComponent, as: 'Profile'}
])

相反,您的配置文件组件将定义 :username 路由参数:

@RouteConfig([
  {path: '/:username/nieuwsfeed', component: FeedComponent, as: 'Feed'},
  {path: '/:username/workouts', component: WorkoutsComponent, as: 'Workouts'}
])

这种方法看起来更直观,并且会减少在嵌套路由组件之间导航的问题。

【讨论】:

  • 首先,感谢您的宝贵时间。听起来不错,这是我昨天尝试过的,它会起作用的。我也认为在这种情况下它更直观。在对此进行了更多研究之后,我找不到任何有关从父组件获取参数的文档。那么我现在可以得出结论,从父组件获取参数不是可行的方法,不是一厢情愿甚至是不可能的吗?
  • 哦,我把我最近发布的关于getting params from parent controllers 的问题弄混了,但这个问题与感兴趣的人非常相关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-28
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多