当在angular中添加断点时,如何仅观察一个组件?
我正在尝试根据窗口大小更改路由。我通过使用断点使其工作,但这是在观察项目的所有页面/组件,而不仅仅是我试图包含多个视图的一个组件。如何仅指定一个最小宽度为900px的组件当在angular中添加断点时,如何仅观察一个组件?,angular,breakpoints,Angular,Breakpoints,我正在尝试根据窗口大小更改路由。我通过使用断点使其工作,但这是在观察项目的所有页面/组件,而不仅仅是我试图包含多个视图的一个组件。如何仅指定一个最小宽度为900px的组件 从'@angular/cdk/layout'导入{BreakpointObserver}; 从'@angular/Router'导入{Router}; 构造函数(专用bpo:BreakpointObserver,专用路由器:router){} @组成部分({ templateUrl:'./list.component.htm
从'@angular/cdk/layout'导入{BreakpointObserver};
从'@angular/Router'导入{Router};
构造函数(专用bpo:BreakpointObserver,专用路由器:router){}
@组成部分({
templateUrl:'./list.component.html',
样式URL:['./list.component.scss'],
提供者:[服务]
})
ngOnInit():void{
此.bpo.observe(['(最小宽度:900px)])
.订阅(结果=>{
if(result.matches){
this.router.navigate([“/list”]);
}否则{
this.router.navigate([“/list expandable”]);
}
});
}
当窗口大小低于900px时,我的所有页面都切换到可扩展列表。我只希望列表窗口在大小低于900px时切换。当窗口缩小时,其余部分应保持不变。您必须取消订阅observable。创建一个私有变量
minWidthSubscription
,并将其设置为从this.bpo.observe.subscribe(…)返回的subscription。然后在ngondestory()
中调用this.minWidthSubscription.unsubscribe()
我收到一个错误:core.js:6014错误:未捕获(承诺中):NullInjectorError:StaticInjectorError(AppModule)[ListComponent->Subscription]:StaticInjectorError(平台:core)[ListComponent->Subscription]:NullInjectorError:没有订阅的提供程序!NullInjectorError:StaticInjectorError(AppModule)[ListComponent->Subscription]:StaticInjectorError(Platform:core)[ListComponent->Subscription]:我更新了答案。您没有在构造函数中注入订阅,这就是为什么会出现静态注入错误。
private minWidthSubscription: Subscription;
constructor(private bpo: BreakpointObserver, private router: Router) {}