如何使用带角度

时间:2017-12-28 12:37:35

标签: javascript css angular materialize

我使用 angular-cli 创建了一个 angular4 项目。我想要materialize-css@next库。所以我用

安装了它
npm install materialize-css@next --save

所以这个安装了

"materialize-css": "^1.0.0-alpha.2",

然后在angular-cli.json中我添加了对css和js文件的引用

"styles": [
   "styles.css",
   "../node_modules/materialize-css/dist/css/materialize.css"
],
"scripts": [
   "../node_modules/materialize-css/dist/js/materialize.js"
],

现在这对于按钮和导航栏等普通组件工作正常,因为这些组件不需要任何js。

如何创建动态元素,如轮播,可折叠和其他需要js的组件?

正如我用谷歌搜索的那样有包装库,如 angualr2-materialize

所以我安装了这个

npm install angular2-materialize --save

并在我的 app.module.ts

中导入了该模块
import { MaterializeModule } from 'angular2-materialize';

然后在imports

@NgModule数组中
imports: [
  BrowserModule,
  MaterializeModule
],

当我使用以下标记时

<a class="waves-effect waves-light btn modal-trigger" (click)="openModal()">Modal</a>

<div id="modal1" class="modal bottom-sheet" materialize="modal" [materializeParams]="[{dismissible: false}]" [materializeActions]="modalActions">
    <div class="modal-content">
        <h4>Modal Header</h4>
        <p>A bunch of text</p>
    </div>
    <div class="modal-footer">
        <a class="waves-effect waves-green btn-flat" (click)="closeModal()">Close</a>
        <a class="modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
    </div>
</div>

显示

index.js:4 Uncaught Error: Couldn't find Materialize object on window. It is created by the materialize-css library. Please import materialize-css before importing angular2-materialize.
    at Object.../../../../angular2-materialize/dist/index.js (index.js:4)
    at __webpack_require__ (bootstrap f20633ecefcae2ee4f21:54)
    at Object.../../../../../src/app/app.module.ts (app.component.ts:9)
    at __webpack_require__ (bootstrap f20633ecefcae2ee4f21:54)
    at Object.../../../../../src/main.ts (environment.ts:8)
    at __webpack_require__ (bootstrap f20633ecefcae2ee4f21:54)
    at Object.3 (main.ts:11)
    at __webpack_require__ (bootstrap f20633ecefcae2ee4f21:54)
    at webpackJsonpCallback (bootstrap f20633ecefcae2ee4f21:25)
    at main.bundle.js:1

我错过了什么吗?

有没有办法在不使用包装器的情况下使用MaterialiseCSS库?

3 个答案:

答案 0 :(得分:8)

angular2-materialize基于materialize-css 0.X。由于materialize-css 1.X不依赖于jQuery。即使我没有找到任何包装,我也不想使用包装模块。所以我通过以下步骤解决了这个问题。

1) angular-cli.json

中的JS和CSS参考
"styles": [
   "styles.css",
   "../node_modules/materialize-css/dist/css/materialize.css"
],
"scripts": [
  "../node_modules/hammerjs/hammer.js",
  "../node_modules/materialize-css/dist/js/materialize.js"
]
某些组件需要

hammerjs来监听滑动事件。

2)导入ts

import * as M from "materialize-css/dist/js/materialize";

3)获取元素参考

@ViewChild('collapsible') elCollapsible: ElementRef;

4)包裹元素

ngAfterViewInit() {
    let instanceCollapsible = new M.Collapsible(this.elCollapsible.nativeElement, {});
}

完成了。

答案 1 :(得分:0)

angular2-materialize可能使用Materialize 0.x,它使用Materialize命名空间。 materialize-next切换到M命名空间。

angular2-materialize应该已经包含Materialise(0.x)。

答案 2 :(得分:0)

1)安装Materialize:

yarn add material-design-icons-iconfont
yarn add materialize-css
yarn add -D @types/materialize-css

2)导入实体化样式:

@import '~material-design-icons-iconfont/dist/material-design-icons.css';
@import '~materialize-css/sass/materialize';

3)为组件创建HTML:

<div class="container">
  <ul id="dropdown" class="dropdown-content">
    <li><a href="#!" (click)="logout()">Logout</a></li>
  </ul>
  <nav>
    <div class="nav-wrapper">
      <a href="#" class="brand-logo">Akita</a>
      <ul id="nav-mobile" class="right hide-on-med-and-down">
        <li><a routerLink="dashboard" *showIfLoggedIn="true">Dashboard</a></li>
        <li><a routerLink="todos" *showIfLoggedIn="true">Todos</a></li>
        <li *showIfLogin="false"><a *showIfLoggedIn="false" routerLink="login"
            class="waves-effect waves-light btn">Login</a></li>
        <li><a class="dropdown-trigger" href="#!" data-target="dropdown" #dropdown>Welcome, {{ name$ | async}}!<i
              class="material-icons right">arrow_drop_down</i></a></li>
      </ul>
    </div>
  </nav>
</div>

4)导入实现并绑定下拉列表:

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import * as M from 'materialize-css';
import { Observable } from 'rxjs';
import { AuthQuery, AuthService } from '../auth/state';

@Component({
  selector: 'app-main-nav',
  templateUrl: './main-nav.component.html',
  styleUrls: ['./main-nav.component.scss']
})
export class MainNavComponent implements OnInit, AfterViewInit {
  name$: Observable<string>;

  @ViewChild('dropdown') dropdown: ElementRef;

  constructor(private router: Router, private authService: AuthService, private authQuery: AuthQuery) {}

  ngOnInit() {
    this.name$ = this.authQuery.name$;
  }

  ngAfterViewInit() {
    new M.Dropdown(this.dropdown.nativeElement, {});
  }

  logout() {
    this.authService.logout();
    this.router.navigateByUrl('');
  }
}