简体   繁体   English

在Polymer 2.0组件中将JavaScript与HTML分离的最佳实践

[英]Best Practice in separating javascript from HTML in Polymer 2.0 components

The typical code example given for a Polymer 2.0 project looks like this: 为Polymer 2.0项目提供的典型代码示例如下所示:

 <link rel="import" href="../../bower_components/polymer/polymer-element.html"> <dom-module id='component-name'> <template> <style> :host { display: block; } </style> <slot></slot> </template> <script> class ComponentName extends Polymer.Element { static get is() { return 'component-name'; } } window.customElements.define(ComponentName.is, ComponentName); </script> </dom-module> 

I want to separate out the javascript, so that the ComponentName is written in a separate file, thus keeping my linters happy and my sanity in check while working on large projects. 我想分离出javascript,以便将ComponentName写在一个单独的文件中,从而在处理大型项目时保持我的快乐和我的理智。

I've come up with this solution: 我想出了这个解决方案:

 <link rel="import" href="../../bower_components/polymer/polymer-element.html"> <dom-module id='component-name'> <template> <style> :host { display: block; } </style> <slot></slot> </template> <script type='module' src='./componentname.js'></script> </dom-module> 

The only difference is that I'm now loading the file from a separate file as a script type='module' (so that I can also import other js files in to that). 唯一的区别是我现在从一个单独的文件中加载文件作为脚本类型='模块'(这样我也可以将其他js文件导入到该文件中)。

It appears to work, but I'm not sure its really the best thing to do. 它似乎有效,但我不确定它真的是最好的事情。 For one, I'm not sure what scope the class ComponentName is being defined in and if there may be conflicts. 首先,我不确定类ComponentName的定义范围以及是否存在冲突。 But, more importantly, I'm not sure how the loading sequence happens. 但是,更重要的是,我不确定加载顺序是如何发生的。 Does the script load on instantiation of the app instead of through lazy loading? 脚本是否在应用程序的实例化时加载而不是通过延迟加载?

Is there some one out there versed in the Polymer world that has experience with this sort of thing and has an idea of best practices in separating out js logic from the the HTML while setting up components? 是否有一些精通Polymer世界的人有这方面的经验,并且在设置组件时有一个从HTML中分离出js逻辑的最佳实践的想法? Or, is the solution I've lined out sufficient? 或者,我列出的解决方案是否足够? Also, any references to learn more about the dynamic load process Polymer goes through would be beneficial. 此外,任何有关聚合物经历的动态加载过程的更多信息的参考都是有益的。

(and I do know that Polymer 3.0 may solve this problem with es6 modules), but unfortunately I can't wait until it comes out. (我知道Polymer 3.0可以用es6模块解决这个问题),但不幸的是我不能等到它出来。

Here is how to externalize JS and CSS. 以下是如何外化JS和CSS。

<link rel="import" href="../bower_components/polymer/polymer-element.html">

<dom-module id="your-element">
  <link rel="import" type="css" href="your-element.css">
  <template>
  </template>
  <script type="text/javascript" src="your-element.js"></script>
</dom-module>

Your class gets defined in the global scope and the JS files gets imported only once because of the HTML import deduping. 您的类在全局范围内定义,并且由于HTML导入重复数据删除,JS文件只导入一次。 When executing the JS code, it will find the corresponding <dom-module> . 执行JS代码时,会找到相应的<dom-module> Also, don't forget 另外,别忘了

customElements.define(YourElement.is, YourElement);

in your JS file. 在你的JS文件中。 Note that <script ...> can be inside or outside the <dom-module> , and the CSS imports must be outside the template. 请注意, <script ...>可以在<dom-module>内或之外,CSS导入必须在模板之外。 The script will be loaded when the HTML file is loaded. 加载HTML文件时将加载该脚本。 Here is some info on lazy loading elements. 这是关于延迟加载元素的一些信息

Looking at this from another perspective: Polymer 2.0 elements can implement a static getter method named template, so you can seperate out the HTML part like this: 从另一个角度看这个:Polymer 2.0元素可以实现一个名为template的静态getter方法,所以你可以像这样分开HTML部分:

class SeperateMarkup extends PolyElement {

    ...

  static get is() { return 'seperate-markup-el'; }

  static get template() {       
    // Maybe, have a look at 
    // Polymer.DomModule.import(SeperateMarkup.is, 'template');
    // to retrieve markup 

    return "<h1>someMarkup</h1>";
  }

    ...

}

Recommended read on this and other Polymer 2.0 aspects: Monica Dinculescus examples on https://glitch.com/edit/#!/aspiring-chauffeur?path=views/index.html:1:0 关于这个和其他Polymer 2.0方面的推荐阅读:Monica Dinculescus上的例子https://glitch.com/edit/#!/aspiring-chauffeur?path=views/index.html:1:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM