How to use materialize-css with angular

前端 未结 3 1925
我在风中等你
我在风中等你 2020-12-16 21:51

I have created an angular4 project with angular-cli. I want to materialize-css@next library. So I have installed it using

np         


        
3条回答
  •  既然无缘
    2020-12-16 22:29

    1) Install Materialize:

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

    2) Import Materialize styles:

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

    3) Create HTML for the component:

    
    

    4) Import Materialize and bind the dropdown:

    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;
    
      @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('');
      }
    }
    

提交回复
热议问题