Angular路由会保持先前的组件显示,并且不完全加载下一个组件。
创始人
2024-10-28 02:00:24
0

在Angular中,路由默认是在加载新组件时销毁先前的组件。如果你想保持先前的组件显示,并且不完全加载下一个组件,你可以使用标签来实现。

首先,在你的父组件的模板中添加一个标签,用于显示路由组件。例如:


然后,在你的路由配置中,将component属性替换为loadChildren属性,并将组件加载到ComponentFactoryResolver中。例如:

import { ComponentFactoryResolver } from '@angular/core';

const routes: Routes = [
  {
    path: 'your-path',
    loadChildren: () => import('./your-component/your-component.module').then(m => m.YourComponentModule)
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

在你的YourComponentModule中,使用ComponentFactoryResolver来加载组件并将其添加到DOM中。例如:

import { ComponentFactoryResolver, ComponentRef, ViewContainerRef, OnInit } from '@angular/core';
import { YourComponent } from './your-component.component';

@Component({
  selector: 'app-your-component',
  template: ''
})
export class YourComponentModule implements OnInit {
  @ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef;
  componentRef: ComponentRef;

  constructor(private componentFactoryResolver: ComponentFactoryResolver) { }

  ngOnInit() {
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(YourComponent);
    this.componentRef = this.container.createComponent(componentFactory);
  }
}

这样,当你导航到your-path时,先前的组件将保持显示,并且新组件将被加载到DOM中。

请注意,这种方法可能会导致性能问题,因为每次导航时都会加载新组件。因此,你应该慎重使用这种方法,并确保你的应用程序可以处理大量组件的加载和销毁。

相关内容

热门资讯

透视模拟器!红龙poker作弊... 透视模拟器!红龙poker作弊指令,德普之星辅助器,总算了解(有挂技巧)1、让任何用户在无需AI插件...
透视讲解!德普之星有没有挂,w... 透视讲解!德普之星有没有挂,wepoker底牌透视脚本下载,教学盘点(有挂方法)1、ai辅助优化,发...
透视脚本!wpk辅助插件叫什么... 透视脚本!wpk辅助插件叫什么,aapoker辅助包,玩家必备科技(有挂攻略);1、每一步都需要思考...
透视辅助!aapoker辅助插... 透视辅助!aapoker辅助插件工具,aapoker透视怎么用,让我来分享经验(有挂攻略)1、透视辅...
透视教程!wepoker辅助下... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去Q群【1067239143】很多玩家在这款游戏中...
透视ai!wpk辅助工具,we... 透视ai!wpk辅助工具,wepoker底牌透视脚本,查到实测辅助(有挂辅助挂)1、ai辅助优化,发...
辅助透视!aapoker辅助器... 辅助透视!aapoker辅助器怎么用,aapoker免费透视脚本,热点讨论(有挂辅助挂)亲,关键说明...
透视美元局!aapoker透视... 透视美元局!aapoker透视怎么用,aapoker辅助工具免费下载,揭秘攻略(有挂透明);1、让任...
透视私人局!aapoker免费... 透视私人局!aapoker免费透视脚本,智星菠萝辅助器,一分钟揭秘(有挂详情);1、在ai机器人技巧...
透视插件!wpk辅助插件叫什么... 透视插件!wpk辅助插件叫什么,德普之星辅助正版,实测发现(有挂透明);1)辅助挂:进一步探索辅助透...