Angular7 - 无法绑定到 'dataSource',因为它不是 'mat-table' 的已知 属性
Angular7 - Can't bind to 'dataSource' since it isn't a known property of 'mat-table'
我正在使用 Angular 7.0.2,我在尝试使用 Angular Material
创建 table 时遇到此错误
Can't bind to 'dataSource' since it isn't a known property of 'mat-table'
应用-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatTableModule } from '@angular/material';
import { HomeComponent } from './home/home.component';
import { ProductionOrderComponent } from './production-order/production-order.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'production-order', component: ProductionOrderComponent },
];
@NgModule({
imports: [
RouterModule.forRoot(routes),
HttpClientModule,
BrowserAnimationsModule,
MatTableModule,
],
exports: [RouterModule]
})
export class AppRoutingModule { }
html
<mat-table [dataSource]="productionOrder" class="mat-elevation-z8">
<ng-container matColumnDef="t_pdno">
<th mat-header-cell *matHeaderCellDef>Production Order</th>
<td mat-cell *matCellDef="let productionOrder">{{ productionOrder.t_pdno }}</td>
</ng-container>
<ng-container matColumnDef="t_mitm">
<th mat-header-cell *matHeaderCellDef>Item</th>
<td mat-cell *matCellDef="let productionOrder">{{ productionOrder.t_mitm }}</td>
</ng-container>
<mat-header-row *matHeaderRowDef="['t_pdno', 't_mitm']"></mat-header-row>
<mat-row *matRowDef="let row; columns: ['t_pdno', 't_mitm'];"></mat-row>
</mat-table>
组件
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-production-order',
templateUrl: './production-order.component.html',
styleUrls: ['./production-order.component.scss']
})
export class ProductionOrderComponent implements OnInit {
public productionOrder
constructor(private http: HttpClient) { }
ngOnInit() {
this.getData()
}
getData() {
this.http.get('http://localhost:3000/api/production-order').subscribe(res => {
console.log(res['data'])
this.productionOrder = res['data']
})
}
}
我尝试了什么:
使用<table mat-table>
代替<mat-table>
import { MatTableModule } from '@angular/material';
组件中
和这些链接:
有人知道如何解决这个问题吗?
您应该在 AppModule
或声明组件的模块中导入 MatTableModule
。
@NgModule({
imports: [
MatTableModule
...
]
})
public class AppModule
或
@NgModule({
imports: [
MatTableModule
...
],
declarations : [ProductionOrderComponent]
})
public class MyModule
我想完成 Sunil 的回答:您应该在 AppModule
或声明组件的模块中导入 MatTableModule
和 CdkTableModule
。
@NgModule({
imports: [
CdkTableModule,
MatTableModule
...
]
})
public class AppModule
如果您认为通过在 app.module.ts 导入 MatTableModule 所做的一切都是正确的,如下所示
import { MatTableModule } from '@angular/material';
@NgModule({
declarations: [
AppComponent,
TopBarComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatTableModule,
CdkTableModule
],
providers: [],
bootstrap: [AppComponent]
})
并在 html 处将 < table > 更改为 < meta-table > 并且仍然无法正常工作,看看你如何编写 dataSource 属性 标签。
it has to be [dataSource] not [datasource]
我正在使用 Angular 7.0.2,我在尝试使用 Angular Material
创建 table 时遇到此错误Can't bind to 'dataSource' since it isn't a known property of 'mat-table'
应用-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatTableModule } from '@angular/material';
import { HomeComponent } from './home/home.component';
import { ProductionOrderComponent } from './production-order/production-order.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'production-order', component: ProductionOrderComponent },
];
@NgModule({
imports: [
RouterModule.forRoot(routes),
HttpClientModule,
BrowserAnimationsModule,
MatTableModule,
],
exports: [RouterModule]
})
export class AppRoutingModule { }
html
<mat-table [dataSource]="productionOrder" class="mat-elevation-z8">
<ng-container matColumnDef="t_pdno">
<th mat-header-cell *matHeaderCellDef>Production Order</th>
<td mat-cell *matCellDef="let productionOrder">{{ productionOrder.t_pdno }}</td>
</ng-container>
<ng-container matColumnDef="t_mitm">
<th mat-header-cell *matHeaderCellDef>Item</th>
<td mat-cell *matCellDef="let productionOrder">{{ productionOrder.t_mitm }}</td>
</ng-container>
<mat-header-row *matHeaderRowDef="['t_pdno', 't_mitm']"></mat-header-row>
<mat-row *matRowDef="let row; columns: ['t_pdno', 't_mitm'];"></mat-row>
</mat-table>
组件
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-production-order',
templateUrl: './production-order.component.html',
styleUrls: ['./production-order.component.scss']
})
export class ProductionOrderComponent implements OnInit {
public productionOrder
constructor(private http: HttpClient) { }
ngOnInit() {
this.getData()
}
getData() {
this.http.get('http://localhost:3000/api/production-order').subscribe(res => {
console.log(res['data'])
this.productionOrder = res['data']
})
}
}
我尝试了什么:
使用
<table mat-table>
代替<mat-table>
import { MatTableModule } from '@angular/material';
组件中
和这些链接:
有人知道如何解决这个问题吗?
您应该在 AppModule
或声明组件的模块中导入 MatTableModule
。
@NgModule({
imports: [
MatTableModule
...
]
})
public class AppModule
或
@NgModule({
imports: [
MatTableModule
...
],
declarations : [ProductionOrderComponent]
})
public class MyModule
我想完成 Sunil 的回答:您应该在 AppModule
或声明组件的模块中导入 MatTableModule
和 CdkTableModule
。
@NgModule({
imports: [
CdkTableModule,
MatTableModule
...
]
})
public class AppModule
如果您认为通过在 app.module.ts 导入 MatTableModule 所做的一切都是正确的,如下所示
import { MatTableModule } from '@angular/material';
@NgModule({
declarations: [
AppComponent,
TopBarComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatTableModule,
CdkTableModule
],
providers: [],
bootstrap: [AppComponent]
})
并在 html 处将 < table > 更改为 < meta-table > 并且仍然无法正常工作,看看你如何编写 dataSource 属性 标签。
it has to be [dataSource] not [datasource]