如何将md-switch添加到angular 4

时间:2017-07-05 15:46:58

标签: angular

我想将AngularJS Material的md-switch添加到我的angular4项目中,但是我收到以下错误:

Uncaught Error: Template parse errors:
'md-switch' is not a known element:
1. If 'md-switch' is an Angular component, then verify that it is part of this module.
2. If 'md-switch' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message

===============================

更新

test.component.html:

<md-slide-toggle>Slide me!</md-slide-toggle>

test.component.ts:

@Component({
  selector: 'test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})

test.module.ts

import { MaterialModule, MdButtonModule, MdSlideToggleModule} from '@angular/material';
import { NgModule } from '@angular/core';
import { TestComponent } from './test.component';

@NgModule({
  declarations: [
    TestComponent
  ],
  imports: [
    MaterialModule, MdSlideToggleModule,
  ],
})
export class TestModule { }

的package.json

{
  "name": "web-app",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/material": "^2.0.0-beta.7",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.1.3",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "2.5.45",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

我缺少什么?

2 个答案:

答案 0 :(得分:2)

您正在将angular1 lib文档用于angular2模块...

如果您已安装this repo,只需将所需功能从材料导入模块,否则安装包:

npm install @angular/material

不要忘记在所选模块中包含功能组件模块:

test.module.ts:

import {MatSlideToggleModule} from '@angular/material/slide-toggle';

    imports:      [ MatSlideToggleModule,

test.component.html:

<mat-slide-toggle>Slide me!</mat-slide-toggle>

test.component.ts:

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

@Component({
  selector: 'test',
  templateUrl: './test.component.html',
})
export class testComponent{}

答案 1 :(得分:1)

首先,angularjs材料与角度4不兼容,而是使用角度材料2.在添加角度材料之前,请检查此问题:

https://github.com/angular/material2/issues/4137

Treeshaking不会发生角度材料。即使您使用一个模块,它也会导入所有内容,这会增加您的构建大小,从而降低性能。所以,我建议使用任何其他库或编写自己的自定义CSS。

您可以使用的一些材料图书馆是: