Angular2 rc1,新路由器和传递数据 [英] Angular2 rc1, new router and passing data

查看:77
本文介绍了Angular2 rc1,新路由器和传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去使用不推荐使用的路由器时,有一些组件路由到同一组件:

Used to have, with deprecated router, a few components that routed to the same component:

某些组件

import {Component, Injector} from 'angular2/core';
import {IDataServiceSome} from './IDataServiceSome';
import {RouteData} from 'angular2/router';

@Component({
    selector: 'Some',
    templateUrl: './Some.html'
})
export class Some {
    Model;
    DataService: IDataServiceVendor;

    constructor(routeData: RouteData, injector: Injector) {
        var dataServiceToken = routeData.get('DataServiceToken');
        this.DataService = injector.get(dataServiceToken);
        this.Model = DataService.getSomeModel();
    }
}

IDataServiceSome

export interface IDataServiceSome {
    getSomeModel(): Object;
}

例如Comp1,但有Comp2,Comp3等...

import {Component} from 'angular2/core';
import {RouteConfigs, Router, ROUTER_DIRECTIVES} from 'angular2/router';

import {DataServiceSome1} from './IDataServiceSome1';

@RouteConfigs([
    { path: '/Some', name: 'Some', component: Some, data: { DataServiceToken: DataServiceSome1 } }])
@Component({
    directives: [ROUTER_DIRECTIVES],
    providers: [DataServiceSome1],
    selector: 'Comp1',
    template:
    `<div>
        <router-outlet></router-outlet>
        <h1>Comp1 routed to Some</h1>
    </div>`
})
export class Comp1{

}

您可能已经猜到了,有许多实现IDataServiceSome的数据服务和许多路由到Some的组件.使用哪种数据服务的选择来自使用injector已知数据令牌路由到Some组件的任何组件.随着rc1版本和新路由器的使用,RouteData已被弃用或删除,但是如何实现此方案呢?

As you might have guessed, there are many data services that implement IDataServiceSome and many components that route to Some. The choice of which data service is used, comes from any component that routes to Some component using a data token known to injector. With rc1 release and new router, the RouteData is deprecated or removed, but how is this scenario implemented moving forward?

推荐答案

等待Angular2重新添加data.就我而言,可以通过DI注入决定需要哪种数据服务的服务.我发现这有点过头了,就像在URL查询字符串中一样,它只是一个传递参数.唯一的区别是,为了更好的体验,用户不应看到该参数.

Wait for Angular2 to add the data back. In my case, a service that decides which data service is needed could be injected via DI. I found that to be an overkill, it simply is a passing parameters just like it's in URL query strings. The only difference was that the parameter shouldn't be visible to user for a better experience.

来源:

http://www.github.com/angular/angular/issues/8515

这篇关于Angular2 rc1,新路由器和传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆