2

I'm poking Angular2 and it's Routing system. I'm creating 'Project Wizard' @Component with 'child' @Components using @RouteConfig and it looks like this:

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']);
    }

}

I need to share a Mock object across 'child' components and I want to understand what my options are. My current understanding is that:

  1. it can be shared using container object which is @Injectable like some Service.
  2. using RouterData. In this case, I would need to unmarshal data from url.

But are there any other ways to pass this object to @Components directly using router?

1 Answer 1

2

No, these two are the available options. I'd suggest a shared service.

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

Comments

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.