Javascript 如何使用Polymer 1.0上的观察者通过onclick函数将数据从一个组件发送到另一个组件?(不使用本地存储)
我想通过onclick函数'response'使用observer将'categorylist'从'submenu list.html'发送到组件'category.html'。我如何在不使用本地存储的情况下执行此操作?请通过编辑代码来显示 /*子菜单-list.html*/Javascript 如何使用Polymer 1.0上的观察者通过onclick函数将数据从一个组件发送到另一个组件?(不使用本地存储),javascript,function,onclick,polymer-1.0,observers,Javascript,Function,Onclick,Polymer 1.0,Observers,我想通过onclick函数'response'使用observer将'categorylist'从'submenu list.html'发送到组件'category.html'。我如何在不使用本地存储的情况下执行此操作?请通过编辑代码来显示 /*子菜单-list.html*/ <link rel="import" href="../../bower_components/polymer/polymer.html"> <dom-module id="submenu-list"&
<link rel="import" href="../../bower_components/polymer/polymer.html">
<dom-module id="submenu-list">
<template>
<paper-menu>
<paper-item><a onclick="response">My Menu Items</a></paper-item>
</paper-menu>
</template>
<script>
Polymer({
is:'submenu-list',
properties: {
categorylist: {
type: Array,
value:[]
}
},
response: function() {
}
})
</script>
</dom-module>
/*category.html*/
<link rel="import" href="../../bower_components/polymer/polymer.html">
<dom-module id="category">
<template>
</template>
<script>
Polymer({
is:'category',
})
</script>
</dom-module>
很快的答案是你不能。至少从你提供的信息来看。由于这些组件似乎没有以某种方式连接,因此组件之间没有直接的通信方式。可能的解决方案是将信息写入本地存储器,但正如您所说,您不想这样做。其他解决方案可能是redux或rest服务,但我想这也不是一个选项。最后一个建议是,这两个组件是兄弟?那么你可以使用默认数据绑定吗?例如:
<submenu-list categorylist="{{categorylist}}"></submenu-list>
<category categorylist="{{categorylist}}"></category>
现在您必须实现_categorylistChanged函数,当categorylist被更改时将调用该函数
_categorylistChanged: function(newValue, oldValue){
//do something
}
不要忘记何时将categorylist更改为call
this.set('categorylist', //your new value)
否则观察员就不会被触发。我昨天借助路由技术解决了这个问题。无论如何,非常感谢:很酷,我现在实现了Redux架构,解决了我所有的问题
this.set('categorylist', //your new value)