简体   繁体   English

工具提示问题,MatTooltip 在 Angular 中不起作用

[英]Tooltip issue, MatTooltip not working in Angular

I am trying to insert a notifications tooltip in my dashboard page, but the tooltip is not working.我正在尝试在我的仪表板页面中插入通知工具提示,但该工具提示不起作用。 I am very new to Angular, so any leads regarding this will be highly appreciated.我是 Angular 的新手,因此非常感谢任何与此相关的线索。

module.ts

..
    import {MatTooltipModule} from '@angular/material';
..

@NgModule({
..
MatTooltipModule
..})

component.html组件.html

    <div class="notifications">
        <i class="fa fa-bell fa-2x" aria-hiden="true" matTooltip="Tooltip!"></i>
    </div>

To use Angular-Material Tooltip you will need:要使用 Angular-Material Tooltip,您需要:

  1. Import the BrowserAnimationsModule and MatTooltipModule :导入BrowserAnimationsModuleMatTooltipModule

app.module.ts app.module.ts

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatTooltipModule } from '@angular/material/tooltip';

@NgModule({
  imports: [
    BrowserAnimationsModule,
    MatTooltipModule,
    // ...
  1. Add tooltip to your component向组件添加工具提示

test.component.html测试组件.html

<div matTooltip="Tooltip!">Hover me</div>
  1. PS If you haven't already installed and imported HammerJs you may need to (Material uses it for some animations and touch gestures): PS 如果您还没有安装和导入 HammerJs,您可能需要(Material 将其用于某些动画和触摸手势):
    npm i -S hammerjs
    npm i -D @types/hammerjs

And in your app.module.js import hammerjs:在你的 app.module.js 中导入hammerjs:

import 'hammerjs'; 

To view full working example see: https://stackblitz.com/angular/keqjqmxbrbg要查看完整的工作示例,请参阅: https : //stackblitz.com/angular/keqjqmxbrbg

Update更新

I found a memory leak in my application, due to extensive use of mat-tooltip (in a loop).由于大量使用 mat-tooltip(在循环中),我在我的应用程序中发现了内存泄漏。 This memory leak can be fixed by removing HammerJS.可以通过删除 HammerJS 来修复此内存泄漏。
For more info and other possible solutions (instead of removing HammerJS) see: https://github.com/angular/material2/issues/4499有关更多信息和其他可能的解决方案(而不是删除 HammerJS),请参阅: https : //github.com/angular/material2/issues/4499

I had a similar problem.我有一个类似的问题。 It was fixed when I added the Material theme CSS.当我添加 Material 主题 CSS 时,它被修复了。

Check the console to see if it has a warning.检查控制台是否有警告。 Try adding the theme CSS to the parent file.尝试将主题 CSS 添加到父文件。

如果你看到一个错误,表明没有找到材质的主题,添加一个材质主题,比如这个,到你的主 CSS 文件的第一行;

@import '@angular/material/prebuilt-themes/deeppurple-amber.css'; 

看到它是这样的,如果你有单独的模块用于延迟加载或其他东西,那么你应该在那里重新导入 MatTooltipModule 。

问题出在导入语句中,这是正确的方法:

import { MatTooltipModule} from '@angular/material/tooltip';

For anyone who is rendering elements inside a *ngFor and using matTooltip, I suggest adding TrackBy function.对于在 *ngFor 中渲染元素并使用 matTooltip 的任何人,我建议添加TrackBy function。 That solved my issue.这解决了我的问题。

" A function optionally passed into the NgForOf directive to customize how NgForOf uniquely identifies items in an iterable. " https://angular.io/api/core/TrackByFunction . function 可选地传递到 NgForOf 指令中,以自定义 NgForOf 如何唯一标识可迭代中的项目。https://angular.io/api/core/TrackByFunction

My problem was that I was trying to use it inside of a mat-label .我的问题是我试图在mat-label内使用它。 Removing it from the label worked.将其从 label 中移除有效。

In my case, the button was disabled, and so apparently the tooltip doesn't display for disabled buttons.在我的例子中,该按钮被禁用,因此显然工具提示不会为禁用的按钮显示。

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

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