我试图了解我在Polymer的领域内做什么甚至是可能的。我当前的示例涉及尝试将名为currency-formatter的npm模块导入到可以调用的Polymer 2.0元素中。
我遇到的问题是虽然我可以导入requirejs来找到npm模块,但是如果没有它给我发出以下错误,我似乎无法运行该模块:
require.js:168未捕获错误:模块名称“currency-formatter”具有 尚未加载上下文:_。使用require([])
以此代码为例:
<link rel="import" href="bower_components/polymer/polymer-element.html">
<script src="node_modules/requirejs/require.js"></script>
<dom-module id="currency-display">
<template>
<style>
:host {
display: inline-block;
}
</style>
<input type="text" value={{currency::input}}></input>
<p>Currency: - {{formatCurrency}}</p>
</template>
<script>
class CurrencyDisplay extends Polymer.Element {
static get is() { return 'currency-display'; }
static get properties() {
return {
currency: {
type: String,
value: 0,
},
formatCurrency: {
type: String,
notify: true,
computed: '_getCurrency(currency)'
},
type: {
type: String,
value: 'USD',
}
};
}
_getCurrency (currency) {
var currencyFormatter = require('currency-formatter');
var formattedCurrency = currencyFormatter.format(parseInt(currency), { code: type });
return formattedCurrency;
}
}
window.customElements.define(CurrencyDisplay.is, CurrencyDisplay);
</script>
</dom-module>
帮助我理解我做错了什么。