javascript – Polymer给我“Uncaught SyntaxError:Unexpected token”
作者:互联网
在下面的Polymer自定义组件中,当调用函数’deleteState’时(所以当我点击“删除”文章项目时)我总是得到“Uncaught SyntaxError:Unexpected token(”来自Chrome.
在Firefox上,我在控制台中收到“SyntaxError:function statement require a name”错误.
<polymer-element name="tw-state" attributes="stateId">
<template>
<style>
...
</style>
<paper-shadow z="1">
<div layout vertical>
<div class="state-header" layout horizontal center-justified>
<div flex style="margin: 10px">
<content select="h3"></content>
</div>
<paper-menu-button>
<paper-icon-button icon="menu" noink></paper-icon-button>
<paper-dropdown class="dropdown">
<core-menu class="menu">
<paper-item>Settings</paper-item>
<paper-item onclick="{{deleteState}}">Delete</paper-item>
</core-menu>
</paper-dropdown>
</paper-menu-button>
</div>
<div class="state-body" layout vertical start>
<content></content>
</div>
<div class="state-footer" layout horizontal center-justified>
<paper-input-decorator label="New task" flex>
<input is="core-input">
</paper-input-decorator>
<paper-button noink="" role="button" tabindex="0">
<core-icon icon="add" aria-label="add" role="img"></core-icon>
</paper-button>
</div>
</div>
</paper-shadow>
</template>
<script>
Polymer({
deleteState: function() {
console.log("Fire event delete-state");
this.fire('delete-state', {id: 'this.stateId'});
}
});
</script>
在查看了文档和大量示例后,我仍然没有看到我在做错的地方……
解决方法:
在您的纸张项目中,您需要polymer event handler
on-click="{{deleteState}}"
而不是正常的onclick DOM处理程序.如果您使用onclick,则字符串“{{deleteState}}”将被评估为JavaScript代码,从而导致上述错误.
如果使用on-click,则Polymer会对此字符串求值,并将deleteState()函数绑定为单击处理程序.
标签:custom-component,javascript,polymer 来源: https://codeday.me/bug/20190830/1766134.html