Skip to content

Commit

Permalink
fix(module:page-header): location inject error (#5013)
Browse files Browse the repository at this point in the history
close #4945
  • Loading branch information
CK110 authored Apr 13, 2020
1 parent 9760238 commit 9073fa5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components/page-header/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
| `[nzTitle]` | Title string | `string \| TemplateRef<void>` | - | - |
| `[nzSubtitle]` | SubTitle string | `string \| TemplateRef<void>` | - | - |
| `[nzBackIcon]` | Custom back icon | `string \| TemplateRef<void>` | - | - |
| `(nzBack)` | Back icon click event | `EventEmitter<void>` | Call [Location[back]](https://angular.io/api/common/Location#back) when the event not subscribed | - |
| `(nzBack)` | Back icon click event | `EventEmitter<void>` | 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 |
Expand Down
2 changes: 1 addition & 1 deletion components/page-header/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
| `[nzTitle]` | title 文字 | `string \| TemplateRef<void>` | - | - |
| `[nzSubtitle]` | subTitle 文字 | `string \| TemplateRef<void>` | - | - |
| `[nzBackIcon]` | 自定义 back icon | `string \| TemplateRef<void>` | - | - |
| `(nzBack)` | 返回按钮的点击事件 | `EventEmitter<void>` | 未订阅该事件时默认调用 [Location[back]](https://angular.cn/api/common/Location#back) | - |
| `(nzBack)` | 返回按钮的点击事件 | `EventEmitter<void>` | 未订阅该事件时默认调用 [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 组成部分
| 元素 | 说明 |
Expand Down
7 changes: 6 additions & 1 deletion components/page-header/page-header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EventEmitter,
Input,
OnChanges,
Optional,
Output,
SimpleChanges,
TemplateRef,
Expand All @@ -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';
Expand Down Expand Up @@ -82,7 +84,7 @@ export class NzPageHeaderComponent implements OnChanges {

@ContentChild(NzPageHeaderBreadcrumbDirective, { static: false }) nzPageHeaderBreadcrumb: ElementRef<NzPageHeaderBreadcrumbDirective>;

constructor(private location: Location, public nzConfigService: NzConfigService) {}
constructor(@Optional() private location: Location, public nzConfigService: NzConfigService) {}

ngOnChanges(changes: SimpleChanges): void {
if (changes.hasOwnProperty('nzBackIcon')) {
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 9073fa5

Please sign in to comment.