From 9073fa5dd527b38286414b5b6dc12f676ed36e9a Mon Sep 17 00:00:00 2001 From: sanshine Date: Mon, 13 Apr 2020 17:17:21 +0800 Subject: [PATCH] fix(module:page-header): location inject error (#5013) close #4945 --- components/page-header/doc/index.en-US.md | 2 +- components/page-header/doc/index.zh-CN.md | 2 +- components/page-header/page-header.component.ts | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/components/page-header/doc/index.en-US.md b/components/page-header/doc/index.en-US.md index e9227abb3c6..cdde68fe844 100644 --- a/components/page-header/doc/index.en-US.md +++ b/components/page-header/doc/index.en-US.md @@ -28,7 +28,7 @@ import { NzPageHeaderModule } from 'ng-zorro-antd/page-header'; | `[nzTitle]` | Title string | `string \| TemplateRef` | - | - | | `[nzSubtitle]` | SubTitle string | `string \| TemplateRef` | - | - | | `[nzBackIcon]` | Custom back icon | `string \| TemplateRef` | - | - | -| `(nzBack)` | Back icon click event | `EventEmitter` | Call [Location[back]](https://angular.io/api/common/Location#back) when the event not subscribed | - | +| `(nzBack)` | Back icon click event | `EventEmitter` | Call [Location[back]](https://angular.io/api/common/Location#back) when the event not subscribed(you need import [RouterModule](https://angular.io/api/router/RouterModule) or register [Location](https://angular.io/api/common/Location))| - | ### Page header sections | Element | Description | diff --git a/components/page-header/doc/index.zh-CN.md b/components/page-header/doc/index.zh-CN.md index dfb17e08e52..2069fb0218f 100644 --- a/components/page-header/doc/index.zh-CN.md +++ b/components/page-header/doc/index.zh-CN.md @@ -29,7 +29,7 @@ import { NzPageHeaderModule } from 'ng-zorro-antd/page-header'; | `[nzTitle]` | title 文字 | `string \| TemplateRef` | - | - | | `[nzSubtitle]` | subTitle 文字 | `string \| TemplateRef` | - | - | | `[nzBackIcon]` | 自定义 back icon | `string \| TemplateRef` | - | - | -| `(nzBack)` | 返回按钮的点击事件 | `EventEmitter` | 未订阅该事件时默认调用 [Location[back]](https://angular.cn/api/common/Location#back) | - | +| `(nzBack)` | 返回按钮的点击事件 | `EventEmitter` | 未订阅该事件时默认调用 [Location[back]](https://angular.cn/api/common/Location#back)(需要引入 [RouterModule](https://angular.io/api/router/RouterModule) 或者注册 [Location](https://angular.io/api/common/Location)))| - | ### Page header 组成部分 | 元素 | 说明 | diff --git a/components/page-header/page-header.component.ts b/components/page-header/page-header.component.ts index 4a16729f021..d80ab953c99 100644 --- a/components/page-header/page-header.component.ts +++ b/components/page-header/page-header.component.ts @@ -14,6 +14,7 @@ import { EventEmitter, Input, OnChanges, + Optional, Output, SimpleChanges, TemplateRef, @@ -22,6 +23,7 @@ import { import { Location } from '@angular/common'; import { NzConfigService, WithConfig } from 'ng-zorro-antd/core/config'; +import { PREFIX } from 'ng-zorro-antd/core/logger'; import { NzPageHeaderBreadcrumbDirective, NzPageHeaderFooterDirective } from './page-header-cells'; const NZ_CONFIG_COMPONENT_NAME = 'pageHeader'; @@ -82,7 +84,7 @@ export class NzPageHeaderComponent implements OnChanges { @ContentChild(NzPageHeaderBreadcrumbDirective, { static: false }) nzPageHeaderBreadcrumb: ElementRef; - constructor(private location: Location, public nzConfigService: NzConfigService) {} + constructor(@Optional() private location: Location, public nzConfigService: NzConfigService) {} ngOnChanges(changes: SimpleChanges): void { if (changes.hasOwnProperty('nzBackIcon')) { @@ -95,6 +97,9 @@ export class NzPageHeaderComponent implements OnChanges { if (this.nzBack.observers.length) { this.nzBack.emit(); } else { + if (!this.location) { + throw new Error(`${PREFIX} you should import 'RouterModule' or register 'Location' if you want to use 'nzBack' default event!`); + } this.location.back(); } }