在Angular中,有没有办法知道一个组件是否已经完成渲染?

huangapple go评论72阅读模式
英文:

Is there any way to know if a component has finished rendering in angular?

问题

I'm sorry, but it seems like you've provided a detailed technical description of an issue related to Angular development. I'll be happy to assist you in understanding and resolving the problem. Could you please clarify if you need help with a specific aspect of this, or if you have any specific questions?

英文:

I'm developing an application with Angular.

I have created a page component that consists of several subcomponents.
I set up each subcomponent to get data via an api call in ngOnInit and then update the screen to match the data.

The problem is that the layout shift is exposed to the user.
Different components take different amounts of time to make the API calls,
the data written to the component is not of a fixed length, so the height of the components is not fixed, resulting in a large layout shift, which is visible to the user in the process.
(After rendering component A, the position of B and C changes)

I tried to fix this by using ngAfterViewInit or other angular lifecycle methods, but even if I adjust the template rendering with ngIf, hidden, etc, the layout change is still visible to the user.

The only way I've found is to use setTimeout to hide the screen for the duration of the layout change.

However, this seems like a bad idea.
Is there any good way to solve this problem except applying skeleton UI, using setTimeout, and emitting events?

https://stackblitz.com/edit/angular-parent-to-child-communication-9ribd2?file=src%2Fapp%2Fapp.module.ts

This is an example created with the same configuration as my project. You can see the rendering process for each list in their project as well.
(Exposing the process of updating templates on the Defaults screen)

I really want to get this fixed.
Any advice would be greatly appreciated

答案1

得分: 1

创建一个组件属性,类似于 dataReady: boolean = false;,然后在组件的主要HTML元素上使用 *ngIf="dataReady"

一旦数据加载完成,将 dataReady 设置为true。这样在过程完成之前,您的组件将不会显示出来。

英文:

One way to do it is this…

Create a property on your components something like dataReady: boolean = false; and then use *ngIf="dataReady" on the main HTML element of your components.

Once the data is loaded, set dataReady to true. This way your components won’t be displayed until the process is complete.

答案2

得分: 1

你正在经历的布局移位问题在网页开发中是一个常见的问题,特别是在渲染动态内容时。解决这个问题的方法是确保每个子组件的容器高度是固定的,并在从API获取数据时使用加载指示器。

解决这个问题的一种方法是使用CSS的Flexbox布局。你可以在父容器上使用CSS的display: flex属性,并将flex-direction属性设置为column以垂直堆叠子组件。然后可以在每个子组件上设置flex-grow属性为1,以确保每个子组件在父容器内占据相等的空间。这将确保容器的高度固定,不会发生布局移位。

以下是如何将这个解决方案应用到你的示例项目中的示例:

<div class="container">
  <app-list-one class="subcomponent"></app-list-one>
  <app-list-two class="subcomponent"></app-list-two>
  <app-list-three class="subcomponent"></app-list-three>
</div>
.container {
  display: flex;
  flex-direction: column;
  height: 500px; /* 为容器设置一个固定高度 */
}

.subcomponent {
  flex-grow: 1; /* 设置每个子组件占据相等的空间 */
  margin-bottom: 20px;
}

除了为容器设置固定高度并使用Flexbox布局外,你还可以使用加载指示器来通知用户正在从API获取数据。这可以通过为每个子组件添加一个加载状态并在获取数据时显示旋转器或进度条来实现。

以下是如何为每个子组件实现加载状态的示例:

<ng-container *ngIf="loading; else content">
  <!-- 在获取数据时显示加载指示器 -->
  <div class="loading-indicator">
    <mat-spinner></mat-spinner>
  </div>
</ng-container>

<ng-template #content>
  <!-- 在数据可用时显示子组件内容 -->
  <div class="subcomponent-content">
    <!-- 子组件内容在这里 -->
  </div>
</ng-template>

在这个示例中,loading变量是一个布尔标志,指示是否正在从API获取数据。当loading为true时,显示加载指示器,当loading为false时,显示子组件内容。

通过使用固定的容器高度、Flexbox布局和加载指示器,你可以确保布局移位最小化,并向用户提示正在从API获取数据。

希望对你有所帮助,一切是否清楚! 在Angular中,有没有办法知道一个组件是否已经完成渲染?

英文:

The layout shift issue you are experiencing is a common problem in web development when rendering dynamic content. The solution to this problem is to ensure that the height of the container for each subcomponent is fixed and to use a loading indicator while the data is being fetched from the API.

One solution to this problem is to use the CSS Flexbox layout. You can use the CSS display: flex property on the parent container and set the flex-direction property to column to stack the subcomponents vertically. Then you can set the flex-grow property on each subcomponent to 1 to ensure that each subcomponent takes up an equal amount of space within the parent container. This will ensure that the height of the container is fixed, and the layout shift will not occur.

Here's an example of how you can apply this solution to your example project:

&lt;div class=&quot;container&quot;&gt;
  &lt;app-list-one class=&quot;subcomponent&quot;&gt;&lt;/app-list-one&gt;
  &lt;app-list-two class=&quot;subcomponent&quot;&gt;&lt;/app-list-two&gt;
  &lt;app-list-three class=&quot;subcomponent&quot;&gt;&lt;/app-list-three&gt;
&lt;/div&gt;
.container {
  display: flex;
  flex-direction: column;
  height: 500px; /* set a fixed height for the container */
}

.subcomponent {
  flex-grow: 1; /* set each subcomponent to take up equal space */
  margin-bottom: 20px;
}

In addition to setting a fixed height for the container and using the Flexbox layout, you can also use a loading indicator to inform the user that data is being fetched from the API. This can be done by adding a loading state to each subcomponent and displaying a spinner or progress bar while the data is being fetched.

Here's an example of how you can implement a loading state for each subcomponent:

&lt;ng-container *ngIf=&quot;loading; else content&quot;&gt;
  &lt;!-- show loading indicator while data is being fetched --&gt;
  &lt;div class=&quot;loading-indicator&quot;&gt;
    &lt;mat-spinner&gt;&lt;/mat-spinner&gt;
  &lt;/div&gt;
&lt;/ng-container&gt;

&lt;ng-template #content&gt;
  &lt;!-- show subcomponent content when data is available --&gt;
  &lt;div class=&quot;subcomponent-content&quot;&gt;
    &lt;!-- subcomponent content goes here --&gt;
  &lt;/div&gt;
&lt;/ng-template&gt;

In this example, the loading variable is a boolean flag that indicates whether data is being fetched from the API. When loading is true, the loading indicator is displayed, and when loading is false, the subcomponent content is displayed.

By using a fixed height for the container, the Flexbox layout, and a loading indicator, you can ensure that the layout shift is minimized, and the user is informed that data is being fetched from the API.

Hope it helps and everything is clear! 在Angular中,有没有办法知道一个组件是否已经完成渲染?

huangapple
  • 本文由 发表于 2023年4月17日 22:55:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定