[英]How to use PopperJs 2 with Angular 13
I added PopperJS 2.11 as dependencie.我添加了 PopperJS 2.11 作为依赖项。 Then I try to use Popper in AppComponent but I'm not sure how to call instance of Popper.
然后我尝试在 AppComponent 中使用 Popper,但我不确定如何调用 Popper 的实例。 Here is my code:
这是我的代码:
app.component.html:应用程序组件.html:
<button #button>Click</button>
<div #tooltip>
lorem ipsum
</div>
app.component.ts:应用程序组件.ts:
import { Component,OnInit, ViewChild} from '@angular/core';
import { createPopper, VirtualElement, Instance } from '@popperjs/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('button') button: Element | VirtualElement;
@ViewChild('tooltip') tooltip: HTMLElement;
ngAfterViewInit(){
createPopper(this.button, this.tooltip, {
modifiers: [
{
name: 'offset',
options: {
offset: [0, 8],
},
},
],
});
}
}
But in console I have: createPopper.js:209 Popper: Invalid reference or popper argument provided. They must be either a DOM element or virtual element.
但在控制台中我有:
createPopper.js:209 Popper: Invalid reference or popper argument provided. They must be either a DOM element or virtual element.
createPopper.js:209 Popper: Invalid reference or popper argument provided. They must be either a DOM element or virtual element.
Update the problem is that you are write更新问题是你在写
**WRONG**
@ViewChild('button') button: Element | VirtualElement;
@ViewChild('tooltip') tooltip: HTMLElement;
With viewChild you get a ElementRef使用 viewChild 你会得到一个 ElementRef
@ViewChild('bt', { static: true }) button!: ElementRef
@ViewChild('tooltip', { static: true }) tooltip!: ElementRef
And use并使用
this.popperInstance = Popper.createPopper(this.button.nativeElement,
this.tooltip.nativeElement)
See a stackblitz with the first example of the tutorial请参阅包含本教程第一个示例的stackblitz
NOTE: Don't forget install @propperjs/core注意:不要忘记安装@propperjs/core
npm i @popperjs/core
see stackblitz参见stackblitz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.