在angular2 rc6上解决警卫将无效

时间:2016-09-14 08:14:48

标签: angular

与angular2 rc6上的决心守卫有什么关系?

这是我的解析器。查看控制台日志打印WTF的位置?那么有效我看到了我想要的数据。

import { Injectable } from '@angular/core';
import { Router, Resolve,ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { ApiService } from '../api_service/api.service';

@Injectable()
export class BasicSetupResolve implements Resolve<any> {

  constructor(private _apiService: ApiService, private router: Router) {}

  resolve(route: ActivatedRouteSnapshot): Observable<any> | Promise<any> | any {
    let id = route.params['id'];
    let model_id = id;
    let endpoint='/test/model/basic';
    return this._apiService.getModel(endpoint,model_id,false).subscribe(api_object => {
      if (api_object) {
        console.log('WTF',api_object);
        return api_object;
      } else { // id not found
        this.router.navigateByUrl('/dashboard(content:models/basic/' +  model_id +')');
        return false;
      }
    });


  }
}

在我的组件中

ngOnInit(){

    this.route.data.forEach((data: { crisis: BasicSetup }) => {
        console.log('WHEREAREYOU',data);
        //this.editName = data.crisis.name;
        //this.crisis = data.crisis;
    });

}

这是我的服务电话:

getModel(endpoint:string,modelId:any,isHash=true){
    console.log(isHash);
    var url = this.getEndpointUrl(endpoint);
     if (isHash==true){
       var result = this._authHttp.get(this.getModelUrl(endpoint,modelId)).map(res => res.json());
     } else {
       var result =this._authHttp.get(this.getModelUrl(endpoint,modelId)).map(res => res.json().results);
     }

        return result;
    }

为什么这不起作用?

这是router.data对象的控制台日志。

WHEREAREYOU Object {api_object: Subscriber}api_object: Subscriber_subscriptions: nullclosed: truedestination: SafeSubscriber_complete: undefined_context: null_error: undefined_next: (api_object)arguments: (...)caller: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
        at Function.remoteFunction (<anonymous>:3:14)]length: 1name: ""prototype: Objectconstructor: (api_object)arguments: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
        at Function.remoteFunction (<anonymous>:3:14)]caller: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
        at Function.remoteFunction (<anonymous>:3:14)]length: 1name: ""prototype: Objectconstructor: (api_object)arguments: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
        at Function.remoteFunction (<anonymous>:3:14)]caller: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
        at Function.remoteFunction (<anonymous>:3:14)]length: 1name: ""prototype: Object__proto__: ()<function scope>__proto__: Object__proto__: ()apply: apply()arguments: nullcaller: nulllength: 2name: "apply"__proto__: ()<function scope>arguments: (...)get arguments: ThrowTypeError()set arguments: ThrowTypeError()bind: bind()call: call()caller: (...)get caller: ThrowTypeError()set caller: ThrowTypeError()constructor: Function()length: 0name: ""toString: toString()Symbol(Symbol.hasInstance): [Symbol.hasInstance]()__proto__: Object<function scope><function scope>ClosureClosureGlobal: Window__proto__: Object__defineGetter__: __defineGetter__()__defineSetter__: __defineSetter__()__lookupGetter__: __lookupGetter__()__lookupSetter__: __lookupSetter__()constructor: Object()hasOwnProperty: hasOwnProperty()isPrototypeOf: isPrototypeOf()propertyIsEnumerable: propertyIsEnumerable()toLocaleString: toLocaleString()toString: toString()valueOf: valueOf()get __proto__: __proto__()set __proto__: __proto__()__proto__: ()<function scope>_parent: null_subscriptions: nullclosed: truedestination: ObjectisStopped: truesyncErrorThrowable: falsesyncErrorThrown: falsesyncErrorValue: null__proto__: Subscriber__tryOrSetError: (parent, fn, value)__tryOrUnsub: (fn, value)_unsubscribe: ()complete: ()constructor: SafeSubscriber(_parent, observerOrNext, error, complete)error: (err)next: (value)__proto__: SubscriptionisStopped: truesyncErrorThrowable: falsesyncErrorThrown: falsesyncErrorValue: null__proto__: Subscription__proto__: Object

1 个答案:

答案 0 :(得分:1)

尝试添加first(),以便在第一个事件之后关闭observable。 不确定这是否可以解决您的问题,但不知何故,似乎路由器不想启动另一个导航而前一个导航仍在进行中等待响应:

resolve(route: ActivatedRouteSnapshot): Observable<any> | Promise<any> | any {
    let id = route.params['id'];
    let model_id = id;
    let endpoint='/test/model/basic';
    return this._apiService.getModel(endpoint,model_id,false).map(api_object => {
      if (api_object) {
        console.log('WTF',api_object);
        return api_object;
      } else { // id not found
        this.router.navigateByUrl('/dashboard(content:models/basic/' +  model_id +')');
        return false;
      }
    }).first();
}