不显示角度材料组件

时间:2017-07-08 13:07:38

标签: angular angular-material2

我是Angular 2和Angular Material的新手我尝试创建一个示例来检查Angular Material但它不起作用。我得到空的html页面。我的代码如下。

应用组件ts:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
}

App Module .ts

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppComponent } from './app.component';
    import {MdSidenavModule,MdMenuModule} from '@angular/material';
    import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; 
    import {NoopAnimationsModule} from '@angular/platform-browser/animations';
    import 'hammerjs';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    MdSidenavModule,
    BrowserAnimationsModule,
    NoopAnimationsModule,
    MdMenuModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})


export class AppModule { }

App Component Html;

<!--The whole content below can be removed with the new code.-->


 <link href="https://unpkg.com/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
 <div>

 <md-sidenav-container>

   <md-sidenav opened="true">
    <button>
     Add
    </button>
   </md-sidenav>
 </md-sidenav-container>


 <md-menu>
  <button md-menu-item> Settings </button>
  <button md-menu-item> Help </button>
</md-menu>
</div>

styles.css的

@import "../node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css";

我的Package.json

{
  "name": "asyncpipe",
  "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.2.6",
    "@angular/cdk": "^2.0.0-beta.8",
    "@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.8",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.1.2",
    "@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-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

我的angular-cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "asyncpipe"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [
        "../node_modules/hammerjs/hammer.min.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

我已按照网络上提到的所有依赖项包含在内。请帮我解决这个问题。

3 个答案:

答案 0 :(得分:2)

import { MaterialModule } from '@angular/material';删除MaterialModuleimports: []。自beta.3 [Changelog]以来已被弃用。

另外,添加NoopAnimationsModule

import {NoopAnimationsModule} from '@angular/platform-browser/animations';

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

我还在Github中找到了您的错误issue。似乎回退到animations版本4.1.3是目前的解决方案。

尝试npm install '@angular/animations"@4.1.3' --save

如果有帮助,请告诉我!

<强>更新

md-menu没有显示,因为它需要mdMenuTriggerFor才能打开菜单。在那之前,它一直隐藏着。此外,要使sidenav生效,所有内容都必须位于md-sidenav-container标记内。 Sidenav docMenu doc

请使用以下内容更改您的html代码:

<md-sidenav-container style="height: 500px">

  <button md-icon-button [mdMenuTriggerFor]="menu">
    <md-icon>more_vert</md-icon>Menu
  </button>


  <md-sidenav opened="true" #sidenav 
      style="background-color: skyblue">
    <button md-raised-button style="margin: 2px 0 0 6px">
     Add
    </button>
    <p>This is Sidenav</p>
   </md-sidenav>

   <md-menu #menu="mdMenu">
     <button md-menu-item (click)="sidenav.open()"> Sidenav </button>
    <button md-menu-item> Settings </button>
    <button md-menu-item> Help </button>
  </md-menu>

</md-sidenav-container>

Demo

答案 1 :(得分:1)

确定。

我认为问题不在于您的代码。根据您的评论,我发现您的某个项目依赖项(@angular/platform-browser)存在问题。有角度报告的错误: https://github.com/angular/angular/issues/17400

尝试将您的依赖项更新为最后一个:4.2.6

为此,您需要运行以下命令:

 npm update

要确保您是最新的,请检查已安装软件包的版本:

npm ls --depth 0

如果您已经通过MaterialModule全部导入,则也无需单独导入每个组件。

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    MaterialModule,
    BrowserAnimationsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

答案 2 :(得分:0)

我遇到了同样的问题。我原来在styles.scss 中没有主题。我添加了一个预先构建的主题和呈现的组件。