gpt4 book ai didi

javascript - 使用路由器将对象传递给 Angular2 组件

转载 作者:搜寻专家 更新时间:2023-10-30 21:42:48 25 4
gpt4 key购买 nike

我在戳 Angular2,它是 Routing 系统。我正在使用 @RouteConfig 创建带有“子”@Component 的“项目向导”@Component,它看起来像这样:

const enum State {
welcome, basicData, groupsData, overview
}

const enum Condition {
back
}

@Component({
selector: 'router-outlet',
templateUrl: '/app/templates/wizard/project/project-wizard-container.html',
directives: [
ROUTER_DIRECTIVES,
],
})

@RouteConfig([
{ path: '/', name: 'ProjectWizardWelcome', component: ProjectWizardWelcomeComponent, useAsDefault: true },
{ path: '/step2', name: 'ProjectWizardStep2', component: ProjectWizardStep2Component },
{ path: '/step3', name: 'ProjectWizardStep3', component: ProjectWizardStep3Component },
{ path: '/overview', name: 'ProjectWizardOverview', component: ProjectWizardOverviewComponent },
])

export class ProjectWizardComponent {

mock: Mock = new Mock();

private mapping: {key: State, value: string}[] = [
{ key: State.welcome, value: 'ProjectWizardWelcome' },
{ key: State.basicData, value: 'ProjectWizardStep2' },
{ key: State.groupsData, value: 'ProjectWizardStep3' },
{ key: State.overview, value: 'ProjectWizardOverview' },
];

private transitions: FSM.Transition<State, Condition>[] = [
{ from: State.welcome, conditions: [], to: State.basicData },
{ from: State.basicData, conditions: [Condition.back], to: State.welcome },
{ from: State.basicData, conditions: [], to: State.groupsData },
{ from: State.groupsData, conditions: [Condition.back], to: State.basicData },
{ from: State.groupsData, conditions: [], to: State.overview },
{ from: State.overview, conditions: [Condition.back], to: State.groupsData },
];

private fsm: FSM<State, Condition> = new FSM(State.welcome, this.transitions);

constructor(
private _router: Router,
private _routeParams: RouteParams) {
}

onPrev(): void {
var prevState = this.fsm.apply([Condition.back]).get();
var prevRoute = this.mapping[prevState].value;
this._router.navigateByInstruction(this._router.generate([prevRoute]), true);
}

onNext(): void {
var nextState: State = this.fsm.apply([]).get();
var nextRoute = this.mapping[nextState].value;
this._router.navigateByInstruction(this._router.generate([nextRoute]), true);
}

onCancel(): void {
this._router.navigate(['Welcome']);
}

}

我需要在“子”组件之间共享一个 Mock 对象,并且我想了解我的选择。我目前的理解是:

  1. 它可以使用@Injectable容器对象来共享,就像一些Service
  2. 使用 RouterData。在这种情况下,我需要从 url 中解码数据。

但是还有其他方法可以直接使用路由器将此对象传递给@Component吗?

最佳答案

不,这两个是可用的选项。我建议共享服务。

关于javascript - 使用路由器将对象传递给 Angular2 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com