简体   繁体   English

使用 angular 实现 css

[英]Materialize-css with angular

I am trying to use Angular 10 with Materializecss我正在尝试将 Angular 10 与Materializecss一起使用

I can see that the styles is loaded correctly, but I am not able to get the JS events for opening sidebar to work properly.我可以看到 styles 已正确加载,但我无法让打开侧边栏的 JS 事件正常工作。

This is what I did so far:这是我到目前为止所做的:

  1. I have installed materialize-css using npm我已经使用npm安装了materialize-css
  2. Added "node_modules/materialize-css/dist/css/materialize.min.css", in styles and "node_modules/materialize-css/dist/js/materialize.min.js" to scripts in angular.json Added "node_modules/materialize-css/dist/css/materialize.min.css", in styles and "node_modules/materialize-css/dist/js/materialize.min.js" to scripts in angular.json
  3. My app.component.ts looks like this:我的app.component.ts看起来像这样:
import { Component, ViewChild, ElementRef } from '@angular/core';
import * as M from "materialize-css/dist/js/materialize";

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

  @ViewChild('collapsible') collapsible: ElementRef;

  ngAfterViewInit() {

    let instanceCollapsible = new M.Collapsible(this.collapsible.nativeElement, {edge: 'left'});
    console.log(instanceCollapsible);

    console.log(this.collapsible.nativeElement);

  }
}
  1. My app.component.html looks like this我的app.component.html看起来像这样
<nav>
  <div class="nav-wrapper">
    <a href="#" class="brand-logo center">Logo</a>
  </div>
</nav>

<ul id="collapsible" #collapsible class="sidenav">
  <li>
    <a href="#!">
      <i class="material-icons">cloud</i>First Link With Icon
    </a>
  </li>
  <li><a href="#!">Second Link</a></li>
  <li><div class="divider"></div></li>
  <li><a class="subheader">Subheader</a></li>
  <li><a class="waves-effect" href="#!">Third Link With Waves</a></li>
</ul>
<a href="#" class="sidenav-trigger" data-target="collapsible">
  <i class="material-icons">menu</i>
</a>

<router-outlet></router-outlet>

<footer>

</footer>

I can't figure out why it doesn't open the menu from the side.我不明白为什么它不能从侧面打开菜单。 When I log instanceCollapsible , I can see that the correct element (UL) has been logged.当我记录instanceCollapsible时,我可以看到已记录了正确的元素 (UL)。

I have solved it the other way.我已经用另一种方式解决了。 I added JS file for Materialize-css in src/assets/js/ folder and registered that file in angular.json under scripts .我在src/assets/js/文件夹中添加了 Materialize-css 的 JS 文件,并在scripts下的angular.json中注册了该文件。

Then in my component I added the initialization然后在我的组件中我添加了初始化

ngOnInit(): void {
    var elems = document.querySelectorAll('.sidenav');
    var instances = M.Sidenav.init(elems, {edge: 'right'});
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM