2016-04-13 15 views
1

Farklı modlarda çalıştırmam gereken CLI kullanılarak oluşturulan bir Angular2 uygulamasına sahibim. Bu modların, sayfa bir IFrame'de barındırılacağı gibi sorgu dizesine geçirilmesi gerekir.Angular2'de Uygulama sınıfındaki RouteParams'e Erişim

Okuduğum kadarıyla, yöneltilen bir bileşende yalnızca mevcut oldukları için üst düzey uygulamada RouteParams'a erişirken bir sorun var.

şu anda javascript location.search nesnesine gidip istediğimi yapıyorum, ancak eğer mümkünse işleri doğru bir şekilde test edebilmem için doğru bağımlılıkta enjekte edilen şekilde yapmayı tercih ederim.

Birisi bana parametrelere erişmek için doğru yolu önerebilir.

@Component({ 
    selector: 'eox-app', 
    providers: [ROUTER_PROVIDERS, RouteParams, 
     FilterService, 
     LoggingService, 
     SpinnerService, 
     StateService, 
    ], 
    templateUrl: 'app/eox.html', 
    styleUrls: ['app//eox.css'], 
    directives: [ROUTER_DIRECTIVES], 
    pipes: [] 
}) 
@RouteConfig([ 

].concat(CliRouteConfig)) 

export class EoxApp { 
    public menuItems = [ 
     { caption: 'Dashboard', link: ['DashboardRoot'] }, 
     { caption: 'Fonds', link: ['FundRoot'] }, 
     { caption: 'Logs', link: ['LogRoot'] }, 
     { caption: 'API', link: ['ApiRoot'] } 
    ]; 

    public isEmbeded = false; 
    constructor(
     private _router: Router, 
     private _log: LoggingService, 
     private _state: StateService, 
     private _routeParams: RouteParams) { 

     this.checkForEmbeded(); 
     let jwt = this.getCookie("eox-token"); 
     this._state.isAuthenticated = jwt === "123456"; 

     if(!this._state.isAuthenticated){ 
      this._log.log("Not authenticated", "Eox vNext"); 
      this._router.navigate(['DashboardRoot']); 
     } 
     else { 
      this._log.log("Authenticated user", "Eox vNext"); 
     } 
     if(this.isEmbeded && this._state.isAuthenticated) 
     { 
      this._log.log("Redirect to fundroot", "Eox vNext"); 
       this._router.navigate(['FundRoot']); 
     } 
    } 

    getCookie(c_name) { 
     var i, x, y, ARRcookies = document.cookie.split(';'); 
     for (i = 0; i < ARRcookies.length; i++) { 
      x = ARRcookies[i].substr(0, ARRcookies[i].indexOf('=')); 
      y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1); 
      x = x.replace(/^\s+|\s+$/g, ""); 
      if (x == c_name) { 
       return unescape(y); 
      } 
     } 
    } 

    checkForEmbeded() { 
     this.isEmbeded = location.search.indexOf("embeded") > 0; 
     this._log.log("Embeded mode " + this.isEmbeded, "Eox vNext"); 
    } 
} 

cevap

0

Sen enjekte edebilir Router (zaten yaptığın gibi) aşağıda gösterildiği gibi, abone ve sonra params erişmek:

constructor(
     private _router: Router, 
     private _log: LoggingService, 
     private _state: StateService, 
     private _routeParams: RouteParams) { 
    this._router.subscribe(path => { 
     console.debug(this._router.currentInstruction.component.params); 
    }); 
} 
+0

umut verici görünüyor ama bir yapı hatası alıyorum: rror: 'typescript aşağıdaki hataları bulundu: D: /EOX2/depots/tmp/broccoli_type_script_compiler-input_base_path-3f2kXEiS.tmp/0/src/client/app/eox.ts (63,28): '{[anahtar: dize argumen ]: dize; } '' string 'türünde parametreye atanamaz. BroccoliTypeScriptCompiler._doIncrementalBuild (D: \ EOX2 \ depoları \ node_modules açısal cli \ lib \ brokoli \ brokoli-tip script.js \: 111: 19) 'Bu kapanması – Mark

+0

yerine bileşen – Mark

+0

daha fonksiyonu belirtir '=> ' –

İlgili konular