【问题标题】:Angular: how resolve the error core.js:6185Angular:如何解决错误 core.js:6185
【发布时间】:2020-06-02 16:36:59
【问题描述】:

我不知道这是什么错误。我使用来自材料的 sidenav 使用 Angular 9。 我有这个错误: core.js:6185 错误错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 'margin-left' 的先前值:'165'。当前值:“172”。

import { Component, OnInit, ViewChild } from "@angular/core";
import { Movie } from "src/app/models/movie.model";
import { MoviesService } from "src/app/services/movies.service";
import { MatPaginator } from "@angular/material/paginator";
import { MatTableDataSource } from "@angular/material/table";
import { MatSnackBar } from "@angular/material/snack-bar";
import { MatDialog, MatDialogConfig } from "@angular/material/dialog";
import { DialogComponent } from "src/app/dialog/dialog.component";
import { MatSort } from "@angular/material/sort";

@Component({
  selector: "app-movie-list",
  templateUrl: "./movie-list.component.html",
  styleUrls: ["./movie-list.component.scss"],
})
export class MovieListComponent implements OnInit {
  displayedColumns: string[] = [
    "poster",
    "title",
    "originalTitle",
    "date",
    "vote",
    "remove",
  ];
  movies: Movie[];
  size: number;

  dataSource: MatTableDataSource<Movie>;
  @ViewChild(MatPaginator) paginator: MatPaginator;

  constructor(
    private moviesService: MoviesService,
    private _snackBar: MatSnackBar,
    public dialog: MatDialog
  ) {}

  ngOnInit(): void {
    this.size = 0;
    this.moviesService.getMovies().subscribe((res) => {
      this.movies = res;
      this.size = this.movies.length;
      this.dataSource = new MatTableDataSource(this.movies);
      this.dataSource.paginator = this.paginator;
    });
  }

  openSnackBar(item: string) {
    this._snackBar.open(item, "Close", {
      duration: 3000,
      horizontalPosition: "right",
      verticalPosition: "bottom",
    });
  }

  remove(movie: Movie) {
    const isMovie = true;
    const dialogConfig = new MatDialogConfig();

    dialogConfig.data = {movie, isMovie};

    const dialogRef = this.dialog.open(DialogComponent, dialogConfig);

    dialogRef.afterClosed().subscribe((data) => {
      if (data === true) {
        this.moviesService.deleteMovie(movie.id).subscribe();
        this.dataSource.data.splice(this.movies.indexOf(movie), 1);
        this.dataSource = new MatTableDataSource<Movie>(this.dataSource.data);
        this.dataSource.paginator = this.paginator;
        this.size--;
        this.openSnackBar("Movie removed !");
      }
    });
  }
}
table {
  width: 100%;
  margin-top: 62px;
}

img {
  height: 140px;
  width: 100px;
  margin-top: 5%;
  margin-bottom: 5%;
}

.update {
  color: grey;
}

.notifications {
  text-align: center;
padding-top: 100px;
margin-left: 10px;
margin-right: 10px;
}

.example-container {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: #eee;
}
<mat-sidenav-container class="example-container">
  <mat-sidenav mode="side" opened>
    <div class="notifications">
      <h3>Movies List</h3>
      <h4 class="number"><span class="badge badge-primary">Total movies : {{ size }} </span></h4>
    </div>
  </mat-sidenav>
  <mat-sidenav-content>
    <div class="mat-elevation-z8">
      <table mat-table [dataSource]="dataSource">
    
        <ng-container matColumnDef="poster">
          <th mat-header-cell *matHeaderCellDef> Poster</th>
          <td mat-cell *matCellDef="let element">
            <div *ngIf="element.poster_path; else noPoster">
              <img src="https://image.tmdb.org/t/p/w200/{{element.poster_path}}" alt="...">
            </div>
            <ng-template #noPoster>
              <img src="../../assets/not_found.jpg" alt="...">
            </ng-template>
          </td>
        </ng-container>
    
        <ng-container matColumnDef="title">
          <th mat-header-cell *matHeaderCellDef> French title </th>
          <td mat-cell *matCellDef="let element"> {{element.title}} </td>
        </ng-container>
    
        <ng-container matColumnDef="originalTitle">
          <th mat-header-cell *matHeaderCellDef> Original title </th>
          <td mat-cell *matCellDef="let element"> {{element.original_title}} </td>
        </ng-container>
    
        <ng-container matColumnDef="date">
          <th mat-header-cell *matHeaderCellDef> Release date </th>
          <td mat-cell *matCellDef="let element"> {{element.release_date | date: 'dd MMMM, y'}} </td>
        </ng-container>
    
        <ng-container matColumnDef="vote">
          <th mat-header-cell *matHeaderCellDef> Vote </th>
          <td mat-cell *matCellDef="let element"> {{element.vote_average}} </td>
        </ng-container>
    
        <ng-container matColumnDef="remove">
          <th mat-header-cell *matHeaderCellDef> Remove </th>
          <td mat-cell *matCellDef="let element">
            <button mat-icon-button color="warn" (click)="remove(element)">
              <mat-icon>delete</mat-icon>
            </button>
          </td>
        </ng-container>
    
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
      </table>
    
      <mat-paginator [pageSizeOptions]="[25, 50, 100]" showFirstLastButtons></mat-paginator>
    </div>
  </mat-sidenav-content>
</mat-sidenav-container>

如果您有解决的想法,我会很高兴。 谢谢

【问题讨论】:

    标签: angular core


    【解决方案1】:

    像这样改变你对孩子的看法:

     @ViewChild(MatPaginator, {static : true}) paginator: MatPaginator;
    

    试试这个:

        ngAfterContentInit() {
        this.size = 0;
        this.moviesService.getMovies().subscribe((res) => {
          this.movies = res;
          this.size = this.movies.length;
          this.dataSource = new MatTableDataSource(this.movies);
          this.dataSource.paginator = this.paginator;
        });
    
    }
    

    【讨论】:

    • 我尝试了但没有任何改变
    • 当我打开电影列表页面时
    • 我已经修改答案请检查
    • 我试过了,抱歉没有结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多