Polymer 1.0:<script> 标签是放在 <dom-module> 里面还是外面?

Polymer 1.0: Do <script> tags go inside or outside the <dom-module>?

问题

Which method of placing the <script> tags is "best-practice?"

  • Inside the <dom-module>?

or

  • Outside the <dom-module>?

另外,请回答:

  1. 为什么?
  2. 你的答案来源是什么?
  3. 以 "wrong" 方式进行有哪些不利风险?

聚合物入门套件:外部

在 Polymer Starter Kit 中,my-list.htmlmy-greeting.html 文件将 <script> 标签 放在 <dom-module> 之外。

像这样:

<dom-module>
  <style>...</style>
  <template>...</template>
<dom-module>
<script>...</script>

其他专家:INSIDE

但是,我从 Google 员工和 Google 开发人员那里听到和看到了几个例子,这些例子建议 <script> 标签应该放在 内部 <dom-module>.

像这样:

<dom-module>
  <style>...</style>
  <template>...</template>
  <script>...</script>
<dom-module>

看起来 <script> 标签应该放在 里面 <dom-module>

Per this definition in the developer guide.

元素定义
<dom-module id="element-name">

  <template>
    <style>
      /* CSS rules for your element */
    </style>

    <!-- local DOM for your element -->

    <div>{{greeting}}</div> <!-- data bindings in local DOM -->
  </template>

  <script>
    // element registration
    Polymer({
      is: "element-name",

      // add properties and methods on the element's prototype

      properties: {
        // declare properties for the element's public API
        greeting: {
          type: String,
          value: "Hello!"
        }
      }
    });
  </script>

</dom-module>

正确答案是 - 没关系。虽然文档确实如@Mowzer 所指出的那样,但这只是一个示例而不是定义。至少有一些实际的 Polymer 元素,例如 e。 G。 iron-image 在 dom-模块之外。

dom-module 和 Polymer 构造函数定义的对象之间的关系是通过传递给 Polymer 构造函数的对象的 'is' 属性 和dom-模块。

来自Local DOM guide

Give the <dom-module> an id attribute that matches its element’s is property and put a inside the <dom-module>. Polymer will automatically clone this template’s contents into the element’s local DOM.

附带说明一下,您还可以成功地使用 将 html 与 JS 分开 - 我只是猜测这是一种可能这个问题的原因。这个(AFAIK)的唯一缺点是,在这种情况下,您的元素的硫化版本将显示不正确的(偏移)代码行号以显示 JS 错误。