0
I was doing a small project with Angular (V8.2.5) and Materialize (materialize-css v1.0.0), and decided at a certain point to insert a dropdown menu into a header component. Following the documentation template, my HTML looked like this:
<div class="right hide-on-med-and-down">
<ul>
<li><a>item1</a></li>
<li><a>item2</a></li>
<li><a>item3</a></li>
<li><a id="dropdown-trigger" target="dropdown1">
<fa-icon [icon]="['fas', 'user']" size="lg"></fa-icon>
</a></li>
</ul>
</div>
<ul id="dropdown1" class="dropdown-content">
<li><a href="#!">one</a></li>
<li><a href="#!">two</a></li>
<li class="divider"></li>
<li><a href="#!">three</a></li>
</ul>
However, when doing Typescript, I don’t know which place is right to call jQuery from the example, and I did this:
import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
/* trecho omitido */
export class HeaderComponent implements OnInit {
/* trecho omitido */
ngOnInit() {
$(document).ready(() => {
$("#dropdown-trigger").dropdown();
});
}
}
However, typescript does not recognize the dropdown function (https://materializecss.com/dropdown.html), and the system does not perform the corresponding action. What should I do?
And what error is returned? Remembering that it is not very advised to use jQuery together with Angular, each manipulates the Dom in its own way and this can generate many bugs, plus more N reasons, but, people insist to use them together, for example, for Angular there is the ngx-materialize to use Materialize: https://www.npmjs.com/package/ngx-materialize
– LeAndrade
ERROR in src/app/core/header/header.component.ts:15:30 - error TS2339: Property 'dropdown' does not exist on type
'JQuery<HTMLElement>'
.$("#dropdown-trigger").dropdown();
wdm : Failed to Compile.– Arthur Siqueira
What makes me more confused is that, according to the documentation of Materialize, this alone would be enough to make the menu work
– Arthur Siqueira
Only that’s enough, maybe with Angular no. You know that Materialize works with pure Javascript tbm right?!
– LeAndrade
I saw the javascript on the site, and I had a question in this excerpt:
var instances = M.Dropdown.init(elems, options);
. What is that classM
, Where is Dropdown.init (an extract from https://materializecss.com/dropdown.html) called? Even Typescript error when searching for class.– Arthur Siqueira
M is Materialize, it’s like a command: Materialize will start the Dropdown component. You have to see which scope is declaring the instance variable, as I said, messing with something that wasn’t made for that is really bad.
– LeAndrade
It worked, I removed the jQuery and imported the Materialize and the function started to respond. It also had a conflict with @types/materialize-css that caused an error calling it without jQuery, but it was just reinstalling. Thank you.
– Arthur Siqueira
Cool Arthur, success there.
– LeAndrade
Hello hello hello, share your own solution in a new response with the most complete code snippet possible, so you can help someone in the future as is the goal of this platform.
– Ernesto Casanova