Angular 嵌套组件路由
Angular nested component routing
我有一个导航栏组件,它有一个 routerLink
<p>
<a routerLink="/user">User</a>
</p>
以及我正在向其传递值的 UserComponent 下的嵌套组件
@Component({
selector: 'app-user',
template: `
<input #box (keyup.enter)="onEnter(box.value)">
<div *ngIf="box.size > 0">
<app-order [value]="value"></app-order>
</div>`
})
export class UserComponent implements OnInit {
value = '';
onEnter(value: string) {
this.value = value;
}
constructor() {
}
ngOnInit() {
}
}
订单组件
@Component({
selector: 'app-order',
template:`{{id}}`
})
export class OrderComponent implements OnInit {
@Input() id:String;
constructor() { }
ngOnInit() {
}
}
现在,当我输入用户 link 时,我会看到 link 和传递的值。当我再次单击 link 时,我得到了具有相同值的页面。我怎样才能改变这种行为,以便当我再次点击路由按钮时,我得到没有传递值的输入?
我在点击导航栏上的相关按钮尝试刷新页面时遇到了同样的问题。
Angular 声明在同一个 URL 上的导航无效,因为应用程序的状态由其当前 URL.
表示
有一个解决方法:onSameUrlNavigation
不幸的是,这个选项只允许守卫和解析器在导航到同一个 URL 时再次 运行,所以如果你激活它,你会注意到......什么都没有!
所以我用这个class创建了自己的解决方法:
/**
* Abstract class that allows derived components to get refreshed automatically on route change.
* The actual use case is : a page gets refreshed by navigating on the same URL and we want the rendered components to refresh
*/
export abstract class AutoRefreshingComponent implements OnInit, OnDestroy {
public routerEventsSubscription: Subscription;
protected router: Router;
constructor() {
this.router = AppInjector.get(Router);
}
/**
* Initialization behavior. Note that derived classes must not implement OnInit.
* Use initialize() on derived classes instead.
*/
ngOnInit() {
this.initialize();
this.routerEventsSubscription = this.router.events.filter(x => x instanceof NavigationEnd).subscribe(res => {
this.initialize();
});
}
/**
* Destruction behavior. Note that derived classes must not implement OnDestroy.
* Use destroy() on derived classes instead.
*/
ngOnDestroy(): void {
this.routerEventsSubscription.unsubscribe();
this.destroy();
}
/**
* Function that allows derived components to define an initialization behavior
*/
abstract initialize(): void;
/**
* Function that allows derived components to define a destruction behavior
*/
abstract destroy(): void;
}
AppInjector 指的是这个:
import {Injector} from '@angular/core';
/**
* Allows for retrieving singletons using `AppInjector.get(MyService)` (whereas
* `ReflectiveInjector.resolveAndCreate(MyService)` would create a new instance
* of the service).
*/
export let AppInjector: Injector;
/**
* Helper to access the exported {@link AppInjector}, needed as ES6 modules export
* immutable bindings; see http://2ality.com/2015/07/es6-module-exports.html
*/
export function setAppInjector(injector: Injector) {
if (AppInjector) {
// Should not happen
console.error('Programming error: AppInjector was already set');
}
else {
AppInjector = injector;
}
}
并且在您的 AppModule 中:
import { setAppInjector } from './app.injector';
// ...
export class AppModule {
constructor(private injector: Injector) {
setAppInjector(injector);
}
}
然后使所有需要的组件扩展 AutoRefreshingComponent
。
你的情况:
@Component({
selector: 'app-user',
template: `
<input #box (keyup.enter)="onEnter(box.value)">
<div *ngIf="box.size > 0">
<app-order [value]="value"></app-order>
</div>`
})
export class UserComponent extends AutoRefreshingComponent {
value = '';
onEnter(value: string) {
this.value = value;
}
constructor() {
}
initialize() {
// Reset your box value
}
destroy() {
}
}
这对于您想要实现的目标来说可能有点矫枉过正,但是每次您想要在同一 URL 导航上刷新组件时它都会很有用。
如果有帮助请告诉我。
我有一个导航栏组件,它有一个 routerLink
<p>
<a routerLink="/user">User</a>
</p>
以及我正在向其传递值的 UserComponent 下的嵌套组件
@Component({
selector: 'app-user',
template: `
<input #box (keyup.enter)="onEnter(box.value)">
<div *ngIf="box.size > 0">
<app-order [value]="value"></app-order>
</div>`
})
export class UserComponent implements OnInit {
value = '';
onEnter(value: string) {
this.value = value;
}
constructor() {
}
ngOnInit() {
}
}
订单组件
@Component({
selector: 'app-order',
template:`{{id}}`
})
export class OrderComponent implements OnInit {
@Input() id:String;
constructor() { }
ngOnInit() {
}
}
现在,当我输入用户 link 时,我会看到 link 和传递的值。当我再次单击 link 时,我得到了具有相同值的页面。我怎样才能改变这种行为,以便当我再次点击路由按钮时,我得到没有传递值的输入?
我在点击导航栏上的相关按钮尝试刷新页面时遇到了同样的问题。
Angular 声明在同一个 URL 上的导航无效,因为应用程序的状态由其当前 URL.
表示有一个解决方法:onSameUrlNavigation
不幸的是,这个选项只允许守卫和解析器在导航到同一个 URL 时再次 运行,所以如果你激活它,你会注意到......什么都没有!
所以我用这个class创建了自己的解决方法:
/**
* Abstract class that allows derived components to get refreshed automatically on route change.
* The actual use case is : a page gets refreshed by navigating on the same URL and we want the rendered components to refresh
*/
export abstract class AutoRefreshingComponent implements OnInit, OnDestroy {
public routerEventsSubscription: Subscription;
protected router: Router;
constructor() {
this.router = AppInjector.get(Router);
}
/**
* Initialization behavior. Note that derived classes must not implement OnInit.
* Use initialize() on derived classes instead.
*/
ngOnInit() {
this.initialize();
this.routerEventsSubscription = this.router.events.filter(x => x instanceof NavigationEnd).subscribe(res => {
this.initialize();
});
}
/**
* Destruction behavior. Note that derived classes must not implement OnDestroy.
* Use destroy() on derived classes instead.
*/
ngOnDestroy(): void {
this.routerEventsSubscription.unsubscribe();
this.destroy();
}
/**
* Function that allows derived components to define an initialization behavior
*/
abstract initialize(): void;
/**
* Function that allows derived components to define a destruction behavior
*/
abstract destroy(): void;
}
AppInjector 指的是这个:
import {Injector} from '@angular/core';
/**
* Allows for retrieving singletons using `AppInjector.get(MyService)` (whereas
* `ReflectiveInjector.resolveAndCreate(MyService)` would create a new instance
* of the service).
*/
export let AppInjector: Injector;
/**
* Helper to access the exported {@link AppInjector}, needed as ES6 modules export
* immutable bindings; see http://2ality.com/2015/07/es6-module-exports.html
*/
export function setAppInjector(injector: Injector) {
if (AppInjector) {
// Should not happen
console.error('Programming error: AppInjector was already set');
}
else {
AppInjector = injector;
}
}
并且在您的 AppModule 中:
import { setAppInjector } from './app.injector';
// ...
export class AppModule {
constructor(private injector: Injector) {
setAppInjector(injector);
}
}
然后使所有需要的组件扩展 AutoRefreshingComponent
。
你的情况:
@Component({
selector: 'app-user',
template: `
<input #box (keyup.enter)="onEnter(box.value)">
<div *ngIf="box.size > 0">
<app-order [value]="value"></app-order>
</div>`
})
export class UserComponent extends AutoRefreshingComponent {
value = '';
onEnter(value: string) {
this.value = value;
}
constructor() {
}
initialize() {
// Reset your box value
}
destroy() {
}
}
这对于您想要实现的目标来说可能有点矫枉过正,但是每次您想要在同一 URL 导航上刷新组件时它都会很有用。
如果有帮助请告诉我。