【问题标题】:How to call a method on athird-party component in stenciljs如何在 stenciljs 中调用第三方组件的方法
【发布时间】:2021-07-01 15:24:46
【问题描述】:

我在我的 stencilJs 应用程序中使用来自 lib 的 LottiePlayer

在他们提到的文档中:

您也可以通过编程方式设置和加载动画。

</lottie-player>
const player = document.querySelector("lottie-player");
player.load("https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json");

// or load via a Bodymovin JSON string/object
player.load(
 '{"v":"5.3.4","fr":30,"ip":0,"op":38,"w":315,"h":600,"nm":"new", ... }'
);

现在在我的模板组件中我正在做

import * as lottiePlayer from "@lottiefiles/lottie-player";


@Component({
    tag: "welcome-page",
    styleUrl: "welcome-page.scss",
    shadow: true,
})
export class WelcomePage {
    private lottie: lottiePlayer.LottiePlayer;

    animate(): void {
        console.log(this.lottie);
        this.lottie.load("https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json");
    }

    render(): any {
        return (
            <Host>
               <lottie-player
                    ref={(el) => this.lottie = el}
                    autoplay
                    mode="normal"
                    style={{ width: "300px", height: "300px" }}
                >
                </lottie-player>
                <idv-button
                            buttonType="primary"
                            onClick={this.animate.bind(this)}>
                            Animate
                </idv-button>
             </Host>
          );
      }

当我运行时,我获得了对 html 元素的引用,但它不是 LottiePlayer 的类型,我可以在其上调用方法。

我也这样做了,但播放器为空

    animate(): void {
        const player: lottiePlayer.LottiePlayer = document.querySelector("lottie-player");
        player.load("https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json");
    }

我该如何解决这个问题?

【问题讨论】:

    标签: web-component stenciljs


    【解决方案1】:

    我是这样解决的,以防其他人有同样的问题

    
        @Element() el;
    
        ...
      
        animate(): void {
            let element: any = this.el.shadowRoot.querySelector("lottie-player");
    
            if (element) {
                element.load("https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      相关资源
      最近更新 更多