我一直在try 进口material 时遇到问题.我已经使用了类似于他们在Material.angular.io上的示例的代码,但是我得到了"Inject()必须从注入上下文中调用……"错误.我只是试图让它作为一个简单的表工作,因为我主要需要专注于设计部分,而不是它背后的广泛功能.
双手
import { AppComponent } from './app/app.component';
import { bootstrapApplication } from '@angular/platform-browser';
bootstrapApplication(AppComponent).catch(err => console.error(err));
App.component.ts
import { Component } from '@angular/core';
import { MatTableModule } from '@angular/material/table';
interface brojInfo {
broj: number,
ime: string,
prezime: string
}
var counter: brojInfo[] = [];
for (var i = 0; i <= 1000; i++) {
counter.push({ broj: i, ime: "Name " + i, prezime: "Surname " + i });
}
@Component({
selector: 'app-root',
templateUrl: './App.component.html',
styleUrl: './app.component.css',
standalone: true,
imports: [MatTableModule]
})
export class AppComponent {
displayedColumns: string[] = ['broj', 'ime', 'prezime'];
dataSource = counter;
}
App.component.html
<table mat-table [dataSource]="dataSource">
<ng-container matColumnDef="broj">
<th mat-header-cell *matHeaderCellDef> Broj </th>
<td mat-cell *matCellDef="let element"> {{element.broj}} </td>
</ng-container>
<ng-container matColumnDef="ime">
<th mat-header-cell *matHeaderCellDef> Ime </th>
<td mat-cell *matCellDef="let element"> {{element.ime}} </td>
</ng-container>
<ng-container matColumnDef="prezime">
<th mat-header-cell *matHeaderCellDef> Prezime </th>
<td mat-cell *matCellDef="let element"> {{element.prezime}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
如果这个问题太简单了,我很抱歉,但我已经看过了谷歌向我展示的所有与这个问题相关的问题,文档本身,我真的无法解决这个问题.这是我的第一次牛仔竞技表演,不仅有Angular ,还有前端本身.谢谢您抽时间见我.
I've tried multiple ways that I could find to possibly write the imports: []
part, even tried to make the app.module.ts file and use it instead of the App.component.ts file import in 双手, but I still had the same error.
编辑:忘了提一下,我还试图将paths:
添加到tsfig.app.json中,并将另一个(忘记了这个词)添加到angular.json中,这对其他一些Peers有效.
编辑2:当我将imports: [MatTableModule]
行放入时,错误特别出现