angular - 垫卡在我的 Angular 应用程序中不起作用

标签 angular typescript angular-material

我正在尝试在我的 Angular 应用程序中创建一个登录页面。

我的代码

<mat-card class="login">
    <mat-card-content>
              <div class="example-small-box mat-elevation-z4">
                  <form class="example-form">
                      <mat-form-field class="email example-full-width">
                        <input  matInput placeholder="Email or User name" [(ngModel)]="username" name="username" required >
                      </mat-form-field>
                    
                      <mat-form-field class="example-full-width">
                        <input matInput placeholder="Password" [(ngModel)]="password"type="password" name="password" required>
                      </mat-form-field>
                    </form>  
  
  
                    <button class="btn" mat-raised-button color="primary" (click)="login()" >Login</button>
                 </div>
           
    </mat-card-content>
  </mat-card> 

看起来像这样。 enter image description here

背景颜色是因为给定的CSS。

垫卡没有显示,空间方向也不正确。我已经导入了所有要求。

应用程序模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AhuComponent } from './ahu/ahu.component';
import { FlexLayoutModule } from '@angular/flex-layout';
import { LoginComponent } from './login/login.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import {MatCardModule} from '@angular/material/card';
import {MatTabsModule} from '@angular/material/tabs';
import {MatFormFieldModule} from '@angular/material/form-field';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {MatInputModule, MatButtonModule} from '@angular/material';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";

@NgModule({
  declarations: [
    AppComponent,
    AhuComponent,
    LoginComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserAnimationsModule,
    BrowserModule,
    AppRoutingModule,
    FlexLayoutModule,
    MatCardModule,
    MatTabsModule,
    MatFormFieldModule,
    FormsModule,
    MatInputModule,
    MatButtonModule,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我之前在另一个项目中使用过相同的代码,效果非常好。但是这次它不起作用。有人可以帮我吗

根据评论编辑,angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "BMS": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/BMS",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "BMS:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "BMS:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "BMS:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "BMS:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "BMS:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "BMS"
}

最佳答案

首先,验证您的 Angular 版本是否适合 Material 版本

如果不是问题,您可能忘记了 CSS,将导入添加到您的 styles.css 文件中。

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

关于angular - 垫卡在我的 Angular 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57775560/

相关文章:

typescript - 类型 'void' 不可分配给类型 'ObservableInput<{}>'

css - 如何更改 Angular Material 表行高?

angularjs - Angular 1.5 组件和 ui-router 1.0 : Add attributes to inserted dom element

angular - 如何防止垫子膨胀板关闭?

angular - 如何使用 2 个参数创建主题(rx.js)?

javascript - 如何以编程方式清除 Angular 6 应用程序上的缓存?

forms - 在 FormBuilder 中使用嵌套对象

angular - 我有时会在 Azure DevOps "Error: Cannot find module ' symbol-observable 中收到这样的错误'"

typescript - 使用 AWS CDK 的 API 网关 GET 方法请求 URL 查询字符串参数

typescript - 如何通过属性的存在或不存在来过滤联合