我正在开发Polymer 2中的应用程序,并且正在测试redux如何使用它来控制数据流。我一直在关注一些简单的例子,并试图产生一个概念证明,我可以使用redux商店将一些数据从一个html移动到另一个html。
在html文件中按下按钮时,我正在运行以下功能:
savePackage() {
const payload = {"Package Name" : "Integrations", "Description" : "A package for the integrations team"};
this.dispatch('add', payload);
}
然后,此函数将一个动作调度到我的redux-store.html,我可以确认我可以通过console.log查看该对象。
在我的redux-store.html中,我运行以下代码:
const reducer = (state, action) => {
switch (action.type) {
case 'UPDATE_PACKAGE':
const payload = action.newdata;
console.log(payload);
return Object.assign({}, state, { payload });
}
};
const store = Redux.createStore(reducer);
const ReduxMixin = PolymerRedux(store);
根据我的理解(通过在线阅读)我应该能够从另一个名为data-model.html的html文件中访问此数组。此文件中的脚本块如下所示:
class DataModel extends ReduxMixin(Polymer.Element) {
static get is() { return 'data-model'; }
static get properties() {
return {
payload: {
type: Array,
statepath: 'payload'
}
};
}
}
customElements.define(DataModel.is, DataModel);
在此文件中的模板标签内,我有以下代码:
<h1>data model [[payload]]</h1>
我相信这应该是从第一个html文件显示数组,但它只显示“数据模型”。我曾尝试在data-model.html中调试console.log,但似乎redux没有从redux商店中提取任何数据。
我已检查了所有导入路径并检查了错别字,但我无法弄清楚为什么这不起作用。任何建议都表示赞赏。
答案 0 :(得分:0)
对此的修复是微不足道的(我怀疑它可能是)。在statePath上需要资本P!
感谢您对此的所有帮助