有人能帮我在Primeng的动态对话框中实现Steps组件吗?我正在处理一个示例项目,我需要在一个对话框中实现登录屏幕,其中包括多个步骤,如在第一步输入基本信息,然后在下一步输入关于用户的更多详细信息.

当我在动态对话框中从一个步骤移动到另一个步骤时,我希望导航到不同的组件.

任何大致的指导都是非常感谢的.谢谢

推荐答案

我认为这样做是可以实现的:

根应用组件 bootstrap :

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h1>Hello from {{ name }}!</h1>
    <router-outlet></router-outlet>
  `,
  imports: [RouterOutlet],
})
export class App {
  name = 'Angular';
}

bootstrapApplication(App, {
  providers: [
    /**
     * DialogService must be provided in the component that is about to open the dialog - the component injector is taken in order to keep router-outlets in the right order
     */
    // DialogService,
    provideAnimations(),
    provideRouter([
      {
        path: '',
        loadComponent: () =>
          import('./components/my-page/my-page.component').then(
            (c) => c.MyPageComponent
          ),
        children: [
          {
            path: '',
            pathMatch: 'full',
            redirectTo: 'step-one',
          },
          {
            path: 'step-one',
            loadComponent: () =>
              import(
                './components/my-page/steps/1-step-one/step-one.component'
              ).then((c) => c.StepOneComponent),
          },
          {
            path: 'step-two',
            loadComponent: () =>
              import(
                './components/my-page/steps/2-step-two/step-two.component'
              ).then((c) => c.StepTwoComponent),
          },
          {
            path: 'step-three',
            loadComponent: () =>
              import(
                './components/my-page/steps/3-step-three/step-three.component'
              ).then((c) => c.StepThreeComponent),
          },
        ],
      },
    ]),
  ],
});

Style.scss:

@import 'primeng/resources/themes/lara-light-blue/theme.css';
@import 'primeng/resources/primeng.css';
@import 'primeicons/primeicons.css';
@import 'primeflex/primeflex.css';

将显示该对话框的页面组件:

<ng-container
  *ngIf="dynamicDialog; else dialogTpl"
  [ngTemplateOutlet]="dynamicDialogTpl"
></ng-container>

<ng-template #dialogTpl>
  <button
    type="button"
    (click)="showDialog()"
    pButton
    icon="pi pi-info-circle"
    label="Show dialog"
    [disabled]="dialogVisible"
  ></button>
  <p-dialog position="top" [(visible)]="dialogVisible">
    <app-steps-dialog></app-steps-dialog>
  </p-dialog>
</ng-template>

<ng-template #dynamicDialogTpl>
  <button
    type="button"
    (click)="showDynamicDialog()"
    pButton
    icon="pi pi-info-circle"
    label="Show dynamic dialog"
    [disabled]="dynamicDialogVisible"
  ></button>
</ng-template>

@Component({
  templateUrl: './my-page.component.html',
  standalone: true,
  imports: [
    ButtonModule,
    DialogModule,
    StepsDialogComponent,
    NgIf,
    NgTemplateOutlet,
  ],
  /**
   * DialogService must be provided in the component that is about to open the dialog - the component injector is taken in order to keep router-outlets in the right order
   */
  providers: [DialogService],
})
export class MyPageComponent implements OnInit {
  /**
   * a switch between dialog an dynamicDialog
   */
  dynamicDialog = true;

  dialogVisible: boolean = true;
  dynamicDialogVisible: boolean = false;

  private destroyRef = inject(DestroyRef);
  private dialogService = inject(DialogService);

  private dynamicDialogRef: DynamicDialogRef | undefined;

  private viewContainerRef = inject(ViewContainerRef);

  ngOnInit() {}

  showDialog(): void {
    if (!this.dynamicDialog) {
      this.dialogVisible = true;
    }
  }

  showDynamicDialog(): void {
    if (this.dynamicDialog) {
      this.dynamicDialogVisible = true;
      this.dynamicDialogRef = this.dialogService.open(StepsDialogComponent, {
        appendTo: this.viewContainerRef.element.nativeElement,
        data: {
          dynamic: true,
        },
      });
      this.dynamicDialogRef.onClose
        .pipe(
          tap(() => {
            this.dynamicDialogVisible = false;
            this.dynamicDialogRef = void 0;
          }),
          takeUntilDestroyed(this.destroyRef)
        )
        .subscribe();
    }
  }
}

步骤对话框组件:

<div class="card">
  <p-steps [model]="items" [readonly]="false"></p-steps>
  <router-outlet></router-outlet>
</div>

@Component({
  selector: 'app-steps-dialog',
  templateUrl: './steps-dialog.component.html',
  standalone: true,
  imports: [StepsModule, ButtonModule, RouterOutlet],
})
export class StepsDialogComponent {
  items: MenuItem[] = [
    { label: 'Step One', routerLink: 'step-one' },
    { label: 'Step Two', routerLink: 'step-two' },
    { label: 'Step Three', routerLink: 'step-three' },
  ];

  dynamicDialogConfig = inject(DynamicDialogConfig, { optional: true });

  constructor() {
    console.log('dynamicDialogConfig', this.dynamicDialogConfig);
  }
}

第一步组件:

<h2>Step One</h2>
<div class="flex justify-content-end">
  <p-button icon="pi pi-chevron-right" routerLink="/step-two"></p-button>
</div>

@Component({
  templateUrl: './step-one.component.html',
  standalone: true,
  imports: [RouterLink, ButtonModule],
})
export class StepOneComponent {}

第二步组件:

<h2>Step Two</h2>
<div class="flex justify-content-between">
  <p-button icon="pi pi-chevron-left" routerLink="/step-one"></p-button>
  <p-button icon="pi pi-chevron-right" routerLink="/step-three"></p-button>
</div>

@Component({
  templateUrl: './step-two.component.html',
  standalone: true,
  imports: [RouterLink, ButtonModule],
})
export class StepTwoComponent {}

和第三步组件:

<h2>Step Three</h2>
<div class="flex justify-content-start">
  <p-button icon="pi pi-chevron-left" routerLink="/step-two"></p-button>
</div>

@Component({
  templateUrl: './step-three.component.html',
  standalone: true,
  imports: [RouterLink, ButtonModule],
})
export class StepThreeComponent {}

最后但并非最不重要的是:) stackblitz@demo

Typescript相关问答推荐

创建一个将任意命名类型映射到其他类型的TypScript通用类型

如何根据参数的值缩小函数内的签名范围?

TypScript在对象上强制执行值类型时推断对象文字类型?

如何从TypScript中的接口中正确获取特定键类型的所有属性?

即使子网是公共的,AWS CDK EventBridge ECS任务也无法分配公共IP地址

防止获取发出不需要的请求

在将对象从一个对象转换成另一个对象时,可以缩小对象的键吗?

有没有办法解决这个问题,类型和IntrinsicAttributes类型没有相同的属性?

如何在Typescript 中输入两个相互关联的变量?

如何在LucideAngular 添加自定义图标以及如何使用它们

为什么在回调函数的参数中不使用类型保护?

在分配给类型的只读变量中维护const的类型

有没有一种方法可以确保类型的成员实际存在于TypeScript中的对象中?

字符串文字联合的分布式条件类型

为什么受歧视的unions 在一种情况下运作良好,但在另一种非常类似的情况下却不起作用?

类型脚本中函数重载中的类型推断问题

如何使对象同时具有隐式和显式类型

TS 排除带点符号的键

可选通用映射器函数出错

在Typescript 中,是否有一种方法来定义一个类型,其中该类型是字符串子集的所有可能组合?