错误TypeError:无法在angular4中读取null的属性'startsWith'

时间:2018-03-28 11:55:22

标签: angular angular4-router

登录我的angular4应用程序时出现以下错误。在我的应用程序中,我使用本地存储概念进行会话。当我们登录我的应用程序时,它将检查本地存储是否为null,如果它为null,它将进入另一条路线,它将是默认路由,任何人都可以请你建议我,谢谢你提前回答。

错误:

 core.js:1448 ERROR TypeError: Cannot read property 'startsWith' of null
        at UrlParser.peekStartsWith (router.js:1553)
        at UrlParser.consumeOptional (router.js:1563)
        at UrlParser.parseRootSegment (router.js:1357)
        at DefaultUrlSerializer.parse (router.js:1204)
        at Router.parseUrl (router.js:5214)
        at Router.navigateByUrl (router.js:5113)
        at SafeSubscriber.eval [as _next] (login.component.ts:130)
        at SafeSubscriber.__tryOrUnsub (Subscriber.js:240)
        at SafeSubscriber.next (Subscriber.js:187)
        at Subscriber._next (Subscriber.js:128)
    defaultErrorLogger @ core.js:1448
    ErrorHandler.handleError @ core.js:1509
    next @ core.js:5493
    schedulerFn @ core.js:4327
    SafeSubscriber.__tryOrUnsub @ Subscriber.js:240
    SafeSubscriber.next @ Subscriber.js:187
    Subscriber._next @ Subscriber.js:128
    Subscriber.next @ Subscriber.js:92
    Subject.next @ Subject.js:56
    EventEmitter.emit @ core.js:4307
    (anonymous) @ core.js:4767
    ZoneDelegate.invoke @ zone.js:388
    Zone.run @ zone.js:138
    NgZone.runOutsideAngular @ core.js:4693
    onHandleError @ core.js:4767
    ZoneDelegate.handleError @ zone.js:392
    Zone.runTask @ zone.js:191
    ZoneTask.invokeTask @ zone.js:496
    invokeTask @ zone.js:1517
    globalZoneAwareCallback @ zone.js:1543
    error (async)
    customScheduleGlobal @ zone.js:1643
    ZoneDelegate.scheduleTask @ zone.js:407
    onScheduleTask @ zone.js:297
    ZoneDelegate.scheduleTask @ zone.js:401
    Zone.scheduleTask @ zone.js:232
    Zone.scheduleEventTask @ zone.js:258
    (anonymous) @ zone.js:1808
    (anonymous) @ http.js:1639
    Observable._trySubscribe @ Observable.js:172
    Observable.subscribe @ Observable.js:160
    MapOperator.call @ map.js:57
    Observable.subscribe @ Observable.js:157
    LoginComponent.login @ login.component.ts:107
    (anonymous) @ LoginComponent.html:96
    handleEvent @ core.js:13542
    callWithDebugContext @ core.js:15051
    debugHandleEvent @ core.js:14638
    dispatchEvent @ core.js:9957
    (anonymous) @ core.js:10582
    (anonymous) @ platform-browser.js:2628
    ZoneDelegate.invokeTask @ zone.js:421
    onInvokeTask @ core.js:4736
    ZoneDelegate.invokeTask @ zone.js:420
    Zone.runTask @ zone.js:188
    ZoneTask.invokeTask @ zone.js:496
    invokeTask @ zone.js:1517
    globalZoneAwareCallback @ zone.js:1543

2 个答案:

答案 0 :(得分:0)

您的本地存储变量似乎为null或您正在创建类型。 首先检查您的本地存储变量是否具有值?

if (localStorage.getItem("User")) {
console.log(localStorage.getItem("User"))
}
else{
console.log("local storage variable is empty")
}

答案 1 :(得分:0)

最后我得到了解决方案,正如我之前提到的,我使用本地存储来维护会话和所有,所以我在下面写了检查本地存储的条件是否为null但是它没有工作所以它进入真实状态当本地存储为空时。那个时候它直接为路由分配null,所以我们得到如上所述的这个错误,然后我用下面提到的完美条件替换它工作正常。谢谢。

错误的条件:

if(localStorage.getItem("userRoute") !== null || localStorage.getItem("userRoute") !== "" ){
      const route = localStorage.getItem("userRoute");
      this.router.navigateByUrl(route);
      localStorage.removeItem("userRoute");
    } else {
      this.router.navigateByUrl('/home/DashBoard');
    }

完美条件:

 if(localStorage.getItem("userRoute") === null || localStorage.getItem("userRoute") === ""){
       this.router.navigateByUrl('/home/DashBoard');
    } else {
      const route = localStorage.getItem("userRoute");
      this.router.navigateByUrl(route);
    }