如何安全地将角材料添加到现有的角应用中?

时间:2018-11-04 14:14:36

标签: angular typescript angular-material

我正在尝试将Angular Material添加到我的应用程序中。我使用此npm命令

安装了Material组件

npm install-保存@ angular / material @ angular / animations @ angular / cdk

我必须将所有角度组件更新为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"

2 个答案:

答案 0 :(得分:0)

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

  1. 现在发布Angular Material 7需要RxJS 6.3。 您应该将RxJS软件包更新为"rxjs": "~6.3.3"。语法稍有变化,因此请查看此update guide

  2. 您是否已根据此step在主.scss文件中包含了基础材料主题文件?

此外,我认为您不是要安装"angular-material": "^1.1.10"软件包。它用于Angular.js。您可以删除它。

答案 1 :(得分:0)

安装角材料

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

ng添加@ angular / material

如果需要显示MatSliderModule

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

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

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

像这样将标签添加到app.component.html中:

运行本地开发服务器:

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