简体   繁体   English

Polymer:如何内联外部脚本

[英]Polymer: how to inline external scripts

I am trying to figure out if it is possible to inline external javascripts in my Polymer elements. 我试图弄清楚是否可以在Polymer元素中内联外部javascript。 I know if I have a link to a stylesheet then it gets merged (from http://www.polymer-project.org/docs/polymer/styling.html ): 我知道是否有指向样式表的链接,然后将其合并(来自http://www.polymer-project.org/docs/polymer/styling.html ):

<polymer-element name="my-element">
  <template>
    <link rel="stylesheet" href="my-element.css">
     ...
  </template>
  <script>
    Polymer('my-element',...);
  </script>
</polymer>

Polymer will automatically inline the my-element.css stylesheet using a : Polymer将使用以下命令自动内联my-element.css样式表:

<polymer-element ...>
  <template>
    <style>.../* Styles from my-element.css */...</style>
     ...
  </template>
  <script>
    Polymer('my-element',...);
  </script>
</polymer>

I am looking for a way to do the same with external javascript files: 我正在寻找对外部javascript文件执行相同操作的方法:

<polymer-element name="my-element">
  <template>
    <link rel="stylesheet" href="my-element.css">
     ...
  </template>
  <script src="my-element.js">
</polymer>

and get something like this: 并得到这样的东西:

<polymer-element ...>
  <template>
    <style>.../* Styles from my-element.css */...</style>
     ...
  </template>
  <script>.../* code from my-element.js */...</script>
</polymer>

I looked at vulcanize ( https://github.com/Polymer/vulcanize ) but it does not seem to be able to do it. 我看着vulcanizehttps://github.com/Polymer/vulcanize ),但它似乎无法做到这一点。

EDIT: 编辑:

let me rephrase what I want to achieve: 让我重新说明我想要实现的目标:

I know that when vulcan processes a HTML page that contains elements, it merges the templates of these elements into the resulting output, and (given the --csp option) it also creates a javascript file where it puts all embedded scripts of the elements. 我知道当vulcan处理包含元素的HTML页面时,它将这些元素的模板合并到结果输出中,并且(使用--csp选项)还将创建一个javascript文件,在其中放置元素的所有嵌入式脚本。 I want to be able to merge into the resulting .js file not only embedded scripts but also scripts that are linked to from my elements. 我希望不仅可以将嵌入脚本而且可以将从我的元素链接到的脚本合并到生成的.js文件中。

It should work as you wrote it, except that <script> requires a closing tag. 它应该像您编写时那样工作,除了<script>需要一个结束标记。 Ie

<polymer-element name="my-element">
  <template>
    <link rel="stylesheet" href="my-element.css">
     ...
  </template>
  <script src="my-element.js"></script>
</polymer>

Fwiw, the script in either case is loaded and executed normally by the browser. 首先,无论哪种情况,脚本都可以由浏览器正常加载和执行。 In that sense, it's different from the stylesheet link which is a feature being simulated by Polymer. 从这个意义上讲,它与stylesheet链接不同,后者是由Polymer模拟的功能。

For the same reason, you can load/run that script anywhere. 出于相同的原因,您可以在任何地方加载/运行该脚本。 The tag doesn't have to be inside the <polymer-element> . 标签不必在<polymer-element>

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

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