无法在html文件中使用MatTable和DataSource:Angularjs7 AngularMaterial 7

无法在html文件中使用MatTable和DataSource:Angularjs7 AngularMaterial 7,angular,angular-material-7,Angular,Angular Material 7,我使用的例子来自 工作演示:() 使用与示例中所述相同的material-modules.ts app.module.ts list.Component.ts:与示例相同:添加了以下导入: 获取2个错误:for和dataSource属性 未捕获错误:模板分析错误:“mat表单字段”不是已知的 要素: 未捕获错误:模板分析错误:无法绑定到“数据源” 因为它不是“table”的已知属性。(” 有人能告诉我我遗漏了什么吗?当组件在AppModule之外的模块中声明时,您需要导入声明它的材质模块。共享您

我使用的例子来自 工作演示:()

  • 使用与示例中所述相同的material-modules.ts
  • app.module.ts
  • list.Component.ts:与示例相同:添加了以下导入:
  • 获取2个错误:for和dataSource属性
  • 未捕获错误:模板分析错误:“mat表单字段”不是已知的 要素:

    未捕获错误:模板分析错误:无法绑定到“数据源” 因为它不是“table”的已知属性。(”


    有人能告诉我我遗漏了什么吗?

    当组件在AppModule之外的模块中声明时,您需要导入声明它的材质模块。

    共享您的代码,您可能没有在
    main.ts
    导入中包含
    DemoMaterialModule
    。@添加了MuhammadShareyar代码,请看一看please@Brian .. 我在主文件即app.module.ts中添加了它。我已经在这里添加了代码。请看一下。@LostJon它可以工作:)如果我已经在主app.module.ts文件中添加了,为什么我需要在这里添加?
        import {A11yModule} from '@angular/cdk/a11y';
        import {DragDropModule} from '@angular/cdk/drag-drop';
        import {PortalModule} from '@angular/cdk/portal';
        import {ScrollingModule} from '@angular/cdk/scrolling';
        import {CdkStepperModule} from '@angular/cdk/stepper';
        import {CdkTableModule} from '@angular/cdk/table';
        import {CdkTreeModule} from '@angular/cdk/tree';
        import {NgModule} from '@angular/core';
        import {
          MatAutocompleteModule,
          MatBadgeModule,
          MatBottomSheetModule,
          MatButtonModule,
          MatButtonToggleModule,
          MatCardModule,
          MatCheckboxModule,
          MatChipsModule,
          MatDatepickerModule,
          MatDialogModule,
          MatDividerModule,
          MatExpansionModule,
          MatGridListModule,
          MatIconModule,
          MatInputModule,
          MatListModule,
          MatMenuModule,
          MatNativeDateModule,
          MatPaginatorModule,
          MatProgressBarModule,
          MatProgressSpinnerModule,
          MatRadioModule,
          MatRippleModule,
          MatSelectModule,
          MatSidenavModule,
          MatSliderModule,
          MatSlideToggleModule,
          MatSnackBarModule,
          MatSortModule,
          MatStepperModule,
          MatTableModule,
          MatTabsModule,
          MatToolbarModule,
          MatTooltipModule,
          MatTreeModule,
          MatFormFieldModule,
        } from '@angular/material';
        
        @NgModule({
          exports: [
            A11yModule,
            CdkStepperModule,
            CdkTableModule,
            CdkTreeModule,
            DragDropModule,
            MatAutocompleteModule,
            MatBadgeModule,
            MatBottomSheetModule,
            MatButtonModule,
            MatButtonToggleModule,
            MatCardModule,
            MatCheckboxModule,
            MatChipsModule,
            MatStepperModule,
            MatDatepickerModule,
            MatDialogModule,
            MatDividerModule,
            MatExpansionModule,
            MatGridListModule,
            MatIconModule,
            MatInputModule,
            MatListModule,
            MatMenuModule,
            MatNativeDateModule,
            MatPaginatorModule,
            MatProgressBarModule,
            MatProgressSpinnerModule,
            MatRadioModule,
            MatSelectModule,
            MatSidenavModule,
            MatSliderModule,
            MatSlideToggleModule,
            MatSnackBarModule,
            MatSortModule,
            MatTableModule,
            MatTabsModule,
            MatToolbarModule,
            MatTooltipModule,
            MatTreeModule,
            PortalModule,
            ScrollingModule,
            MatFormFieldModule,
            MatRippleModule
          ]
        })
        export class AppMaterialModule {}
    
    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    import { HttpClientModule } from '@angular/common/http';
    
    import { AppRoutingModule } from  './app-routing.module';
    /* Import material Angular */
    import { AppMaterialModule } from './material-module';
    import { AppComponent } from './app.component';
    
    /* Call Custom Modules */
    import { InstitutionsModule } from './institutions/institutions.module';
    import {SchoolsModule} from './schools/schools.module';
    import { DepartmentsModule } from './departments/departments.module';
    import { ProgramsModule } from './programs/programs.module';
    
    import { BookComponent } from './book/book.component';
    import { BookDetailComponent } from './book-detail/book-detail.component';
    import { BookCreateComponent } from './book-create/book-create.component';
    import { BookEditComponent } from './book-edit/book-edit.component';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    import { PagesComponent } from './pages/pages.component';
    import { HeaderComponent } from './header/header.component';
    import { SidebarComponent } from './sidebar/sidebar.component';
    import { FooterComponent } from './footer/footer.component';
    
    @NgModule({
      declarations: [
        AppComponent,
        BookComponent,
        BookDetailComponent,
        BookCreateComponent,
        BookEditComponent,
        PagesComponent,
        HeaderComponent,
        SidebarComponent,
        FooterComponent
      ],
      imports: [
        BrowserModule,
        AppMaterialModule,
        AppRoutingModule,
        InstitutionsModule,
        SchoolsModule,
        DepartmentsModule,
        ProgramsModule,
          FormsModule,
          ReactiveFormsModule,
          HttpClientModule,
          BrowserAnimationsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    
    
    import { Component, OnInit, ViewChild } from '@angular/core';
    import { MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
    
    export interface UserData {
      id: string;
      name: string;
      progress: string;
      color: string;
    }
    
    /** Constants used to fill up our data base. */
    const COLORS: string[] = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple',
      'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray'];
    const NAMES: string[] = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack',
      'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper',
      'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth'];
    
    /**
     * @title Data table with sorting, pagination, and filtering.
     */
    
    /** Builds and returns a new User. */
    function createNewUser(id: number): UserData {
      const name =
          NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' +
          NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.';
    
      return {
        id: id.toString(),
        name: name,
        progress: Math.round(Math.random() * 100).toString(),
        color: COLORS[Math.round(Math.random() * (COLORS.length - 1))]
      };
    }
    
    
    @Component({
      selector: 'app-school-list',
      templateUrl: './school-list.component.html',
      styleUrls: ['./school-list.component.css']
    })
    
    
    export class SchoolListComponent implements OnInit {
    
      displayedColumns: string[] = ['id', 'name', 'progress', 'color'];
      dataSource: MatTableDataSource<UserData>;
    
      @ViewChild(MatPaginator) paginator: MatPaginator;
      @ViewChild(MatSort) sort: MatSort;
    
      constructor() {
        // Create 100 users
        const users = Array.from({length: 100}, (_, k) => createNewUser(k + 1));
    
        // Assign the data to the data source for the table to render
        this.dataSource = new MatTableDataSource(users);
      }
    
      ngOnInit() {
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
      }
    
      applyFilter(filterValue: string) {
        this.dataSource.filter = filterValue.trim().toLowerCase();
    
        if (this.dataSource.paginator) {
          this.dataSource.paginator.firstPage();
        }
      }
    
    }
    
    <!--mat-form-field>
      <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
    </mat-form-field-->
    
    <div class="mat-elevation-z8">
      <table mat-table [dataSource]="dataSource" matSort>
    
        <!-- ID Column -->
        <ng-container matColumnDef="id">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
          <td mat-cell *matCellDef="let row"> {{row.id}} </td>
        </ng-container>
    
        <!-- Progress Column -->
        <ng-container matColumnDef="progress">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Progress </th>
          <td mat-cell *matCellDef="let row"> {{row.progress}}% </td>
        </ng-container>
    
        <!-- Name Column -->
        <ng-container matColumnDef="name">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
          <td mat-cell *matCellDef="let row"> {{row.name}} </td>
        </ng-container>
    
        <!-- Color Column -->
        <ng-container matColumnDef="color">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Color </th>
          <td mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.color}} </td>
        </ng-container>
    
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;">
        </tr>
      </table>
    
      <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
    </div>
    
    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { SchoolListComponent } from './school-list/school-list.component';
    import { SchoolCreateComponent } from './school-create/school-create.component';
    import { SchoolUpdateComponent } from './school-update/school-update.component';
    import { SchoolViewComponent } from './school-view/school-view.component';
    import { SchoolsRoutingModule } from  './schools-routing.module';
    
    @NgModule({
      declarations: [SchoolListComponent, SchoolCreateComponent, SchoolUpdateComponent, SchoolViewComponent],
      imports: [
        CommonModule,
        SchoolsRoutingModule
      ]
    })
    export class SchoolsModule { }