解决方法: 在Angular 8中,当路由更改时,mat-select的值可能不会被保存。为了解决这个问题,你可以使用Angular的路由守卫来保存mat-select的值。
首先,你需要在你的组件中创建一个变量来保存mat-select的值。例如:
selectedValue: any;
然后,在你的组件中,你可以使用Angular的OnInit生命周期钩子来为这个变量赋值。例如:
import { ActivatedRoute } from '@angular/router';
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.queryParams.subscribe(params => {
this.selectedValue = params['selectedValue'];
});
}
接下来,你需要在你的模板中使用[(ngModel)]来绑定mat-select的值。例如:
Option 1
Option 2
Option 3
最后,你可以使用Angular的路由守卫来保存mat-select的值。在你的路由配置中,你可以为你的路由添加一个canDeactivate守卫,并在守卫中保存mat-select的值。例如:
import { CanDeactivate } from '@angular/router';
export class SaveValueGuard implements CanDeactivate {
canDeactivate(component: any) {
component.route.queryParams.subscribe(params => {
params['selectedValue'] = component.selectedValue;
});
return true;
}
}
请注意,你可能需要在你的路由配置中添加SaveValueGuard。例如:
import { SaveValueGuard } from './save-value.guard';
const routes: Routes = [
{
path: 'your-component',
component: YourComponent,
canDeactivate: [SaveValueGuard]
}
];
这样,当你的路由更改时,mat-select的值就会被保存下来了。