首页 > 解决方案 > 获取 TypeError:使用 ngx-notification-bar 时无法读取未定义的属性(读取“取消订阅”)

问题描述

我已经使用这个ngx-notification-bar 文档中提到的步骤实现了通知栏

app.component.ts文件中,有以下部分代码

this.notificationBarService.create({ message: '<b>Free Shipping </b>', type: NotificationType.Info, allowClose: true, autoHide: false, hideOnHover: false, isHtml: true });

app.component.html文件中,有以下部分代码

<notification-bar
  [ngStyle]="{ height: '3px', background: 'white' }"
></notification-bar>

我可以在我的页面上看到通知栏。但是当我从页面转到任何其他页面(或组件)时app.component.html,我在控制台中收到以下错误,并且组件没有加载,而是有一个空白屏幕

core.js:6228 ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'unsubscribe')
TypeError: Cannot read properties of undefined (reading 'unsubscribe')
    at NotificationBarComponent.ngOnDestroy (ngx-notification-bar.js:165)
    at executeOnDestroys (core.js:14405)
    at cleanUpView (core.js:14304)
    at destroyViewTree (core.js:14073)
    at destroyLView (core.js:14255)
    at RootViewRef.destroy (core.js:15120)
    at ComponentRef$1.destroy (core.js:34191)
    at RouterOutlet.deactivate (router.js:9122)
    at ActivateRoutes.deactivateRouteAndOutlet (router.js:4023)
    at ActivateRoutes.deactivateRouteAndItsChildren (router.js:3981)
    at resolvePromise (zone-evergreen.js:798)
    at resolvePromise (zone-evergreen.js:750)
    at zone-evergreen.js:860
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Object.onInvokeTask (core.js:41632)
    at ZoneDelegate.invokeTask (zone-evergreen.js:398)
    at Zone.runTask (zone-evergreen.js:167)
    at drainMicroTaskQueue (zone-evergreen.js:569)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:484)
    at invokeTask (zone-evergreen.js:1621)

据我了解,这与不调用有关ngOnDestroy()。谁能帮我解决这个问题?

标签: angular

解决方案


推荐阅读