计算与聚合物的结合

Computed Binding with Polymer

这是我的代码摘录:

<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 识别。有人可以帮忙吗?

您似乎正确地声明和使用了 _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>