我开始了一个基于 Angular 17 的新项目,我使用 Angular Material 作为组件,但图标没有显示。我已经检查了所有内容,但我不知道问题出在哪里。 我在index.html中添加了材质库的CSS,但什么也没有。这是集成图标和其他文件的页面示例。
主页.html
<mat-toolbar color="primary">
<button mat-icon-button class="example-icon" aria-label="Example icon-button with menu icon">
<mat-icon>menu</mat-icon>
</button>
<span>Digisoft</span>
<span class="example-spacer"></span>
<span >{{person.firstName}} {{person.lastName}}</span>
<!-- <i class="fa-solid fa-user"></i>-->
<button mat-icon-button [matMenuTriggerFor]="menu" aria-label="Example icon-button with a menu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #menu="matMenu">
<button mat-menu-item (click)="openDialogPass()">
<mat-icon>webhook</mat-icon>
<span>Change password</span>
</button>
<button mat-menu-item (click)="openDialogEdit(person)">
<i class="fa-sharp fa-solid fa-pen"></i>
<span>Edit information</span>
</button>
<button mat-menu-item (click)="logOut()">
<mat-icon>logout</mat-icon>
<span>Deconnexion</span>
</button>
</mat-menu>
</mat-toolbar>
主页.ts
import {Component, Inject} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialog, MatDialogConfig} from "@angular/material/dialog";
import {ChangePasswordDialogComponent} from "../../components/change-password-dialog/change-password-dialog.component";
import {EditDataDialogComponent} from "../../components/edit-data-dialog/edit-data-dialog.component";
import {MatToolbar, MatToolbarModule} from "@angular/material/toolbar";
import {MatButton, MatButtonModule, MatIconButton} from "@angular/material/button";
import {MatIcon, MatIconModule} from "@angular/material/icon";
import {MatMenu, MatMenuItem, MatMenuModule, MatMenuTrigger} from "@angular/material/menu";
import {UserCreateFields} from "../../../auth-api/src-api/models/UserType/user-create-fields";
import {AuthService} from "../../../services/authService/auth.service";
import {Router} from "@angular/router";
import {SliderComponent} from "../../components/slider/slider.component";
import {faArrowLeft} from "@fortawesome/free-solid-svg-icons";
import {FontAwesomeModule} from "@fortawesome/angular-fontawesome";
@Component({
selector: 'app-home-page',
standalone: true,
imports: [
MatIconModule,
FontAwesomeModule,
SliderComponent,
MatMenuItem,
MatMenu,
MatToolbar,
MatIconButton,
MatIcon,
MatMenuTrigger,
],
templateUrl: './home-page.component.html',
styleUrl: './home-page.component.scss'
})
export class HomePageComponent {
slides: any[] = [
{
url: '../../../assets/image/img_3.svg',
title: 'IT Development Company',
description: 'Your partner for innovative solutions',
link: '#atouts',
linkContent: 'Nos atouts',
},
{
url: '../../../assets/image/img_2.svg',
title: 'Hosting and Maintenance',
description: '',
link: '#heb-main',
linkContent: 'Details',
},
{
url: '../../../assets/image/img_1.svg',
title: 'Web development',
description: '',
link: '#dev-web',
linkContent: 'Details',
},
{
url: '../../../assets/image/img.svg',
title: 'Mobile Development',
description: '',
link: '#dev-mob',
linkContent: 'Details',
},
];
constructor( private authService:AuthService,
private router:Router,
private dialog: MatDialog,) {
}
currentIndex = 0;
nextSlide() {
this.currentIndex = (this.currentIndex + 1) % 4;
}
prevSlide() {
this.currentIndex = (this.currentIndex - 1 + 4) % 4;
}
person: UserCreateFields = {email: "", lastName:"fdfd", firstName: "dfdf", password: ""}
openDialogPass() {
this.dialog.open(ChangePasswordDialogComponent, {
height: '90%',
width: '30%',
disableClose:false,
});
}
openDialogEdit(person: any) {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = false;
dialogConfig.autoFocus = true;
dialogConfig.height = '75%';
dialogConfig.width = '90%';
dialogConfig.data= {
person: person,
};
this.dialog.open(EditDataDialogComponent, dialogConfig);
const dialogRef = this.dialog.open(EditDataDialogComponent, dialogConfig);
dialogRef.afterClosed().subscribe(
data => {
if (data=="ok"){
// this.userService.findByEmail(person.email).subscribe(value => {
// this.person = value
// })
}
}
);
dialogRef.close();
}
logOut() {
// this.dialog.open(ConfirmDeleteDialogComponent, {
// disableClose:false,
// data:{
// message: "Voulez-vous quitter l'application ?"
// }
// }).afterClosed().subscribe(value => {
// if (value=='oui'){
// this.authenservice.disconect();
// this.router.navigate(['']);
// }
// });
}
protected readonly faArrowLeft = faArrowLeft;
}
样式.scss
/* You can add global styles to this file, and also import other style files */
@use 'sass:map';
@use '@angular/material' as mat;
/* You can add global styles to this file, and also import other style files */
@import "styles/themeProp";
//@import "styles/custom-theme";
/* Importing Bootstrap SCSS file. */
// 1. Include functions first (so you can manipulate colors, SVGs, calc, etc)
@import "../node_modules/bootstrap/scss/functions";
// 2. Include any default variable overrides here
// 3. Include remainder of required Bootstrap stylesheets
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/variables-dark";
// 4. Include any default map overrides here
// 5. Include remainder of required parts
@import "../node_modules/bootstrap/scss/maps";
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/root";
// 6. Optionally include any other parts as needed
@import "../node_modules/bootstrap/scss/utilities";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/containers";
@import "../node_modules/bootstrap/scss/grid";
@import "../node_modules/bootstrap/scss/helpers";
// 7. Optionally include utilities API last to generate classes based on the Sass map in `_utilities.scss`
@import "../node_modules/bootstrap/scss/utilities/api";
@import "styles/reset";
html, body { height: 100%;}
body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }
.form {
min-width: 150px;
max-width: 500px;
width: 100%;
}
.field {
width: 100%;
}
.mat-card{
border: 1rem #ff4081;
}
.grid{
background-color: #3f51b5;
}
.mat-input-element, .mat-form-field, .mat-form-field-wrapper {
width: 150px;
//max-width: 600px;
}
.btn-out{
color: #3f51b5;
}
package.json
{
"name": "auth-landing-page",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"auth-api": " npm run auth-api:openDocApi3-gen",
"auth-api:openDocApi3-gen": "ng-openapi-gen --input src/auth-api/documentation/swagger.json --output src/auth-api/src-api"
},
"private": true,
"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/cdk": "^17.3.1",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/material": "^17.3.1",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/router": "^17.3.0",
"@fortawesome/angular-fontawesome": "^0.14.0",
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-brands-svg-icons": "^6.4.2",
"@fortawesome/free-regular-svg-icons": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@tinymce/tinymce-angular": "^7.0.0",
"bootstrap": "^5.3.3",
"font-awesome": "^4.7.0",
"ng-openapi-gen": "^0.51.0",
"rxjs": "~7.8.0",
"tinymce": "^6.7.3",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "^17.3.1",
"@angular/cli": "^17.3.1",
"@angular/compiler-cli": "^17.3.0",
"@types/jasmine": "~5.1.0",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.4.2"
}
}
角度.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"authLandingPage": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/auth-landing-page",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": [
"zone.js"
],
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"@angular/material/prebuilt-themes/pink-bluegrey.css",
"../node_modules/font-awesome/css/font-awesome.css",
"src/styles.scss",
"src/styles/custom-theme.scss"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"buildTarget": "authLandingPage:build:production"
},
"development": {
"buildTarget": "authLandingPage:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"buildTarget": "authLandingPage:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": [
"zone.js",
"zone.js/testing"
],
"tsConfig": "tsconfig.spec.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"@angular/material/prebuilt-themes/pink-bluegrey.css",
"src/styles.scss"
],
"scripts": []
}
}
}
}
}
}
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AuthLandingPage</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body class="mat-typography">
<app-root></app-root>
</body>
</html>
mat-icon 的格式已更改。
https://material.angular.io/components/icon/examples
你应该这样做:
<mat-icon fontIcon="webhook"></mat-icon>