【问题标题】:How to access functions defined in js file inside the template of Polymer element?如何访问 Polymer 元素模板内的 js 文件中定义的函数?
【发布时间】:2017-12-21 12:22:31
【问题描述】:

我在 global.function.js 文件中创建了一个函数作为

function getData(flag) {
if (flag === 1) {
  return "one";
 } 
else {
  return "not one";
 }
}

然后使用 custom-js-import.html 元素导入:

<script src="global.function.js"></script>

当我尝试在 custom-element.html 中访问上述函数时,我可以在脚本部分访问它,但不能在模板部分访问它。 有什么方法可以访问 HTML 元素中的函数?

<!-- custom-element.html -->
<link rel="import"  href="https://polygit.org/components/polymer/polymer-element.html">
<link rel="import" href="custom-js-import.html">

<dom-module id="custom-element">

  <template>
    <div>
      Hello
    </div>
    <div id="data"></div>
    <div>{{getData(1)}}</div><!-- Unable to access this from here -->
    <div>{{getLocalData()}}</div>
  </template>

<script>
  // Define the class for a new element called custom-element
  class CustomElement extends Polymer.Element {
    static get is() { return "custom-element"; }
    constructor() {
        super();
      }

      ready(){
        super.ready();
        this.$.data.textContent = "I'm a custom-element.";
        console.log(getData(1));//can be easily accessed from here
      }

      getLocalData(){
        return "local";
      }

  }
  // Register the new element with the browser
  customElements.define(CustomElement.is, CustomElement);
</script>
</dom-module>

Sample Code

【问题讨论】:

    标签: javascript polymer-1.0 mixins polymer-2.x polymer-starter-kit


    【解决方案1】:

    感谢Rico Kahler 建议我使用mixin。使用mixin 解决了我的问题。您可以查看完整的工作示例here

    所有的全局函数都可以在mixin中定义。

    <!--custom-mixin.html-->
    <script>
      const CustomMixin = superclass => class extends superclass {
    
    static get properties() {
      return {};
    }
    
    connectedCallback() {
      super.connectedCallback();
    }
    
    getData(flag) {
       if (flag === 1) {
        return "one(From Mixin)";
       } else {
        return "not one(From Mixin)";
       }
      }
     };
    </script>
    

    记得导入 mixin 文件并将该 mixin 添加到您的元素中。

    <!-- custom-element.html -->
    <link rel="import" href="https://polygit.org/components/polymer/polymer-element.html">
    <link rel="import" href="custom-mixin.html">
    
    <dom-module id="custom-element">
    
      <template>
        <div>
          Hello
        </div>
        <div id="data"></div>
        <div>{{getData(1)}}</div>
        <!-- Unable to access this from here -->
        <div>{{getLocalData()}}</div>
      </template>
    
      <script>
        // Define the class for a new element called custom-element
        class CustomElement extends CustomMixin(Polymer.Element) {
            static get is() {
              return "custom-element";
            }
            constructor() {
              super();
            }
    
            ready() {
              super.ready();
              this.$.data.textContent = "I'm a custom-element.";
              console.log(getData(1)); //can be easily accessed from here
            }
    
            getLocalData() {
              return "local";
            }
    
          }
          // Register the new element with the browser
        customElements.define(CustomElement.is, CustomElement);
      </script>
    </dom-module>
    

    【讨论】:

      【解决方案2】:

      有什么方法可以访问 HTML 元素中的函数吗?

      不是真的。为了在模板中使用数据,您需要将其绑定到属性(Polymer 调用此数据绑定)。

      Polymer 的数据绑定系统旨在将值绑定到模板。这些值通常只是文字(例如字符串和数字)或普通的 ole javascript 对象,例如{a: 'someval', b: 5}Polymer 的数据绑定系统并非旨在将函数绑定到模板,您不能只在模板内使用 javascript。(If you're really into that idea, check out React as a replacement to polymer)

      执行您想做的事情的聚合物方法是使用computed property。与其在模板中调用函数,不如创建一个计算属性来响应其他变量的变化。当属性的状态发生变化时,计算的属性也会发生变化。这个状态可以被认为是你的函数的参数。

      我认为最好只是看看代码是否正常工作(在 chrome 中测试)?

      <link rel="import" href="https://polygit.org/components/polymer/polymer-element.html">
      <link rel="import" href="custom-js-import.html">
      
      <dom-module id="custom-element">
      
        <template>
          <div>
            Hello
          </div>
          <label>
            <input type="number" value="{{flag::input}}">
          </label>
          <h1>from flag: [[flag]]</h1>
          <div id="data"></div>
          <div>{{boundComputedData}}</div><!-- Unable to access this from here -->
          <div>{{getLocalData()}}</div>
        </template>
      
        <script>
          // Define the class for a new element called custom-element
          class CustomElement extends Polymer.Element {
            static get is() {
              return "custom-element";
            }
            constructor() {
              super();
            }
      
            getData(flag) {
              const flagAsNumber = parseInt(flag);
              if (flagAsNumber === 1) {
                return "one";
              } else {
                return "not one";
              }
            }
      
            ready() {
              super.ready();
              this.$.data.textContent = "I'm a custom-element.";
              console.log(this.getData(1)); //can be easily accessed from here
            }
      
            getLocalData() {
              return "local";
            }
      
      
      
            static get properties() {
              return {
                flag: {
                  type: Number,
                  value: 0
                },
                boundComputedData: {
                  type: String,
                  computed: 'getData(flag)'
                }
              };
            }
      
          }
          // Register the new element with the browser
          customElements.define(CustomElement.is, CustomElement);
        </script>
      </dom-module>
      
      <custom-element></custom-element>

      所以我在这里做的是:

      创建一个计算属性boundComputedData 并将computed 属性设置为'getData(flag)',这将使它使用类函数getData

      现在只要属性flag 的状态发生变化,计算的属性就会更新。

      希望对你有帮助!

      【讨论】:

      • 感谢 Rico,我目前正在使用类似的计算属性概念。但是,问题是我需要在八个自定义元素中实现相同的东西。所以,我试图避免重复。
      • @Ofisora 尝试使用 behaviormixin 然后
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      相关资源
      最近更新 更多