如何在 Polymer 2 中使用 intro.js?

How to use intro.js with Polymer 2?

我想在我的 Polymer 2 应用程序中使用 intro.js 作为首次用户体验。下面是我尝试使用的代码,但它对我不起作用。

<base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<script src="intro.js/minified/intro.min.js"></script>
<link rel="import" type="css" href="intro.js/minified/introjs.min.css">

<dom-module id="my-app">
   <template>
      <style>
      </style>
      <p class="line1" data-intro='Hello step one!' data-step="1">Line 1</p>
      <p class="line2" data-intro='Hello step two!' data-step="2">Line 2</p>
      <p class="line3" data-intro='Hello step three!' data-step="3">Line 3</p>
      <p class="line4" data-intro='Hello step four!' data-step="4">Line 4</p>
   </template>
</dom-module>

connectedCallback(){
   super.connectedCallback();
   introJs().start();
}

这是相同的 Codepen。由于 intro.js 不是 Polymer Web 组件,所以我不知道如何将它与 Polymer 2 一起使用。

它不会像你现在那样工作

旁白

  • 如果要将外部样式导入到聚合物元素中,则需要使用 style-modules 阅读 here ,从今以后,最好。
  • link rel=import type=css 已弃用,但如果您仍想使用它们,应将它们放在您的 dom-module 内部,让聚合物填充它。
  • intro.js ,不能限定在您的聚合物元素范围内。 JS 是通过 IIFE 限定范围的,您可能必须编写一个包装器来为任何外部脚本自己做这件事

也就是说,如果您严格希望所有内容都在范围内。

关于你的问题

问题是,intro.js 无法访问您的元素内的 DOM。 因此,它无法访问您的任何 P 标签

一个解决方案是使用 slots

将它们移动/分发到文档本身等外部范围
<dom-module id="my-app">
  <template>
    <!-- uncomment this, if you have a style module pointing to intro'scss only-->
    <!--<style include="intro-css"></style>-->
    <slot>

    </slot>
  </template>
</dom-module>

现在,您需要做的就是将所有 DOM 放入标签 <my-app></my-app>

只有这样 intro.js 才能处理它们。 plunker