与聚合物计算结合

时间:2016-09-22 02:34:32

标签: polymer polymer-starter-kit

这是我的代码的摘录:

<template is="dom-bind">
  <iron-ajax auto
    url="####"
    params=""
    handle-as="json"
    last-response="{{ajaxResponse}}"></iron-ajax>
    <template is="dom-repeat" items="[[ajaxResponse.Items]]">
      <div>                                                
        [[_formatDate(item.ID.N)]]
      </div>                                        
    </template>
 </template>
 ...
 <script>
    Polymer({
        is: 'home-view',
        _formatDate: function(ID) {
            console.log("TEST");
            return "TEST";
        }
    });
</script>

我收到此控制台警告:

[Warning] [dom-bind::_annotatedComputationEffect]: – "compute method `_formatDate` not defined" (data:text/javascript;charset=utf-8,(fu…%0A, line 265, x10)

所以我似乎不知道如何正确定义_formatDate,以便Polymer识别它。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您似乎正确地声明并使用_formatDate()

警告来自dom-bind,仅适用于index.html中的绑定,而不是dom-module内的绑定。您应该从顶部模板中删除is="dom-bind"

<head>
  <base href="https://polygit.org/polymer+1.6.0/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="polymer/polymer.html">
</head>

<body>
  <home-view></home-view>

  <dom-module id="home-view">
    <template>
      <template is="dom-repeat" items="[[items]]">
        <div>[[_formatDate(item)]]</div>
      </template>
    </template>
    <script>
      // For cross-browser compatibility, HTMLImports.whenReady()
      // needed in index.html only
      HTMLImports.whenReady(function() {
        Polymer({
          is: 'home-view',
          properties: {
            items: {
              type: Array,
              value: function() { return ['hello', 'world']; }
            }
          },
          _formatDate: function(id) {
            console.log('id', id);
            return id;
          }
        });
      });
    </script>
  </dom-module>
</body>