Using external JS libraries in a web component

前端 未结 2 1294
忘掉有多难
忘掉有多难 2021-02-07 07:24

I am developing a web component using Polymer 2, and would like to make use of a third-party JavaScript library, which was not specifically designed for use with web components.

2条回答
  •  醉酒成梦
    2021-02-07 07:50

    If you have an external library that does things like document.querySelector then you have two choices.

    1. Choose to not use ShadowDOM with any of your components. If that is not an option, or if you really, REALLY want to use shadowDOM then:
    2. You need to modify the third party library to allow a root element to be specified instead of always using document.

    Beyond those two options you will probably not be able to use a third party library that assumes document will work for everything.

    I guess the other option is to re-evaluate the third party library and see if it is REALLY worth using.

    On my team we don't use third party libraries that are not just solid logic. Things like moment.js are just logic and we can use them without problems.

    But something like jQuery? Yuck! I can't see needing something like that for a component.

    Good luck!

提交回复
热议问题