gpt4 book ai didi

angular - 在 Angular 中使用 Tippy.js

转载 作者:行者123 更新时间:2023-12-02 20:04:12 29 4
gpt4 key购买 nike

我有一个包含以下代码的指令

import { Directive, Input, OnInit, ElementRef, SimpleChanges, OnChanges } from '@angular/core';
import tippy from 'tippy.js';

@Directive({
selector: '[tippy]'
})
export class TippyDirective implements OnInit, OnChanges {

@Input('tippyOptions') public tippyOptions: Object;

private el: any;
private tippy: any = null;
private popper: any = null;

constructor(el: ElementRef) {
this.el = el;
}

public ngOnInit() {
this.loadTippy();
}

public ngOnChanges(changes: SimpleChanges) {
if (changes.tippyOptions) {
this.tippyOptions = changes.tippyOptions.currentValue;
this.loadTippy();
}
}

public tippyClose() {
this.loadTippy();
}

private loadTippy() {
setTimeout(() => {
let el = this.el.nativeElement;
let tippyOptions = this.tippyOptions || {};

if (this.tippy) {
this.tippy.destroyAll(this.popper);
}

this.tippy = tippy(el, tippyOptions, true);
this.popper = this.tippy.getPopperElement(el);
});
}
}

并按如下方式使用指令

<input tippy [tippyOptions]="{
arrow: true,
createPopperInstanceOnInit: true
}" class="search-input" type="text"
(keyup)="searchInputKeyDown($event)">

如何让 Tippy 显示在 mouseenter 或 focus 上,因为这些是默认触发器,来自指令中的 Tippy 实例,这就是我输入 console.log(this.tippy) 时得到的结果 第 44 行

{
destroyAll:ƒ destroyAll()
options:{placement: "top", livePlacement: true, trigger: "mouseenter focus", animation: "shift-away", html: false, …}
selector:input.search-input
tooltips:[]
}

当我尝试使用时遇到错误

this.popper = this.tippy.getPopperElement(el);

ERROR TypeError: _this.tippy.getPopperElement is not a function

当我从 github 中的存储库中获取该指令时,如何才能使该指令发挥作用

https://github.com/tdanielcox/ngx-tippy/blob/master/lib/tippy.directive.ts

我在这里缺少什么,感谢任何帮助,谢谢

最佳答案

我不确定他们试图在您包含的链接存储库中完成什么任务。为了让 tippy.js 正常工作,您应该能够将指令更改为以下内容:

import { Directive, Input, OnInit, ElementRef } from '@angular/core';
import tippy from 'tippy.js';

@Directive({
/* tslint:disable-next-line */
selector: '[tippy]'
})
export class TippyDirective implements OnInit {

@Input('tippyOptions') public tippyOptions: Object;

constructor(private el: ElementRef) {
this.el = el;
}

public ngOnInit() {
tippy(this.el.nativeElement, this.tippyOptions || {}, true);
}
}

Working example repo

关于angular - 在 Angular 中使用 Tippy.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51788380/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com