【问题标题】:How to add safely Angular Material to an existing angular application?如何安全地将 Angular Material 添加到现有的 Angular 应用程序中?
【发布时间】:2019-04-08 01:35:53
【问题描述】:

我正在尝试将 Angular Material 添加到我的应用程序中。我使用这个 npm 命令安装了 Material 组件

npm install --save @angular/material @angular/animations @angular/cdk

我不得不将我所有的 Angular 组件更新到 7.0.2 版本。 我添加了一个新的材料模块并从我的 app.module.js 中引用它

material.module.ts

import { NgModule } from '@angular/core';

import {
  MatButtonModule,
  MatMenuModule,
  MatToolbarModule,
  MatIconModule,
  MatCardModule
} from '@angular/material';

@NgModule({
  imports: [
    MatButtonModule,
    MatMenuModule,
    MatToolbarModule,
    MatIconModule,
    MatCardModule
  ],
  exports: [
    MatButtonModule,
    MatMenuModule,
    MatToolbarModule,
    MatIconModule,
    MatCardModule
  ]
})
export class MaterialModule {}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { FormsModule } from '@angular/forms';

import { AppComponent } from "./app.component";
import { AppRoutingModule } from "./app-routing.module";

import { MaterialModule } from './material.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';



@NgModule({
    imports: [BrowserModule, AppRoutingModule, HttpModule, FormsModule, MaterialModule, BrowserAnimationsModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]

})
export class AppModule { }

这是我的 package.json

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "build": "tsc -p src/",
    "build:watch": "tsc -p src/ -w",
    "build:e2e": "tsc -p e2e/",
    "serve": "lite-server -c=bs-config.json",
    "serve:e2e": "lite-server -c=bs-config.e2e.json",
    "prestart": "npm run build",
    "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
    "pree2e": "npm run build:e2e",
    "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
    "preprotractor": "webdriver-manager update",
    "protractor": "protractor protractor.config.js",
    "pretest": "npm run build",
    "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
    "pretest:once": "npm run build",
    "test:once": "karma start karma.conf.js --single-run",
    "lint": "tslint ./src/**/*.ts -t verbose"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@angular/animations": "^7.0.2",
    "@angular/cdk": "^7.0.2",
    "@angular/common": "~7.0.2",
    "@angular/compiler": "~7.0.2",
    "@angular/core": "~7.0.2",
    "@angular/forms": "~7.0.2",
    "@angular/http": "~7.0.2",
    "@angular/material": "^7.0.2",
    "@angular/platform-browser": "~7.0.2",
    "@angular/platform-browser-dynamic": "~7.0.2",
    "@angular/router": "~7.0.2",
    "angular-in-memory-web-api": "~0.3.0",
    "angular-material": "^1.1.10",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "rxjs": "^5.4.2",
    "systemjs": "0.19.40",
    "typescript": "^3.1.6",
    "typings": "^2.1.1",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.46",
    "canonical-path": "0.0.2",
    "concurrently": "^3.2.0",
    "jasmine-core": "~2.4.1",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "lite-server": "^2.2.2",
    "lodash": "^4.16.4",
    "protractor": "~4.0.14",
    "rimraf": "^2.5.4",
    "tslint": "^3.15.1"
  },
  "repository": {}
}

我清理了 node_modules 并重新安装了所有内容以使我拥有所有最新文件。当我尝试构建我的项目时,我会遇到各种错误。主要错误来自 **\@angular\core** 或 **\@angular\common**

我花了太多时间,显然我遗漏了一些东西,因为它不应该那么复杂。

"errors"

【问题讨论】:

  • 您之前使用的 Angular 版本是什么?您可以按照本指南进行升级。 update.angular.io

标签: angular typescript angular-material


【解决方案1】:

我可以看到两个潜在的问题:

  1. Angular Material 7 的发布现在需要 RxJS 6.3。 你应该将你的 RxJS 包更新为"rxjs": "~6.3.3"。语法发生了一些变化,所以看看这个update guide

  2. 您是否按照此step 将基础材料主题文件包含在您的主.scss 文件中?

另外,我不认为您打算安装 "angular-material": "^1.1.10" 软件包。它适用于 Angular.js。你可以删除它。

【讨论】:

    【解决方案2】:

    安装 Angular 材质

    通过运行以下命令,使用 Angular CLI 的安装示意图来设置您的 Angular Material 项目:

    ng 添加@angular/material

    如果需要显示 MatSliderModule

    您需要通过将以下行添加到您的 app.module.ts 文件中来导入要显示的 MatSliderModule。

    从'@angular/material/slider'导入{ MatSliderModule };

     @NgModule ({....
     imports: [...,
     MatSliderModule,
     …]
     })
    

    将标签添加到 app.component.html 中,如下所示:

    运行本地开发服务器:

    服务 并将浏览器指向http://localhost:4200

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 2017-12-14
      • 2018-09-16
      • 2016-12-28
      • 2017-12-22
      • 1970-01-01
      • 2022-12-03
      • 2019-10-14
      相关资源
      最近更新 更多