ERROR TypeError: Cannot read property 'startsWith' of null in angular4
ERROR TypeError: Cannot read property 'startsWith' of null in angular4
当我登录我的 angular4 应用程序时出现以下错误。在我的应用程序中,我为会话使用本地存储概念。当我们登录到我的应用程序时,它将检查本地存储是否为空,如果它为空,则它会走一条路线,否则它将走默认路线,任何人都可以建议我,谢谢你提前回答。
错误:
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
您的本地存储变量似乎为空或者您正在创建一个类型。
首先检查你的本地存储变量是否有值?
if (localStorage.getItem("User")) {
console.log(localStorage.getItem("User"))
}
else{
console.log("local storage variable is empty")
}
最后我得到了解决方案,正如我之前提到的,我正在使用本地存储来维护会话和所有内容,因此我在下面写了检查本地存储是否为 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);
}
当我登录我的 angular4 应用程序时出现以下错误。在我的应用程序中,我为会话使用本地存储概念。当我们登录到我的应用程序时,它将检查本地存储是否为空,如果它为空,则它会走一条路线,否则它将走默认路线,任何人都可以建议我,谢谢你提前回答。
错误:
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
您的本地存储变量似乎为空或者您正在创建一个类型。 首先检查你的本地存储变量是否有值?
if (localStorage.getItem("User")) {
console.log(localStorage.getItem("User"))
}
else{
console.log("local storage variable is empty")
}
最后我得到了解决方案,正如我之前提到的,我正在使用本地存储来维护会话和所有内容,因此我在下面写了检查本地存储是否为 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);
}