函数 属性 不是 Polymer 中的函数

Function property is not a function in Polymer

我在我的项目中使用 Polymer 框架,在该项目中我在属性中声明了一个函数回调并尝试从另一个函数调用它。但是在访问它时出现错误:

Uncaught TypeError: this.callback is not a function

请看看这个。

Polymer({
  is: "parent-dom",
  properties: {
    people: {
      type: String,
      value: "df"
    },
    item: {
      type: String,
      value: "asdf",
      notify: true
    },
    callback: {
      type: Object,
      value: function(index) {
        console.log("Inside  callback function");
      }
    },

  },
  showTargetColorDialog: function(e) {
    this.callback("sadf");
  }
});

您能否提供有关您想要实现的目标的更多详细信息,因为将聚合物属性指定为函数这不是很常见的情况?

因此您可以在您的元素上声明 public 方法,就像您对 showTargetColorDialog 所做的那样,并且可以像这样调用它们:

document.querySelector('parent-dom').showTargetColorDialog();

但又不是很"Polymer way"。

回答你的原始问题,如果你真的需要设置回调as Polymer 属性(我还没有知道为什么),但你可以:

callback: {
  type: Object,
  value: function() {
    return function(index) {
        console.log("Inside callback function ", index);
    };
  }
},

然后你就可以调用this.callback('something');