gpt4 book ai didi

未显示 Angular Material 组件

转载 作者:行者123 更新时间:2023-12-04 01:43:56 24 4
gpt4 key购买 nike

我是 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';
}

应用模块 .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 { }

应用组件 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>

样式.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": {}
}
}

我已经包含了 web 上提到的所有依赖项。请帮我解决这个问题。

最佳答案

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

另外,添加 NoopAnimationsModule

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

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

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

试试 npm install '@angular/animations"@4.1.3' --save
让我知道这是否有帮助!

更新:
md-menu没有出现,因为它需要 mdMenuTriggerFor可以打开菜单。在那之前,它一直处于隐藏状态。此外,对于 sidenav工作,一切都需要在 md-sidenav-container标签。 Sidenav doc , Menu 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

关于未显示 Angular Material 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com