【问题标题】:Polymer inter-component data binding?聚合物组件间数据绑定?
【发布时间】:2015-10-13 10:39:00
【问题描述】:

我有一个登录组件,我想让登录状态可用于我的应用程序中的其他组件。

谁能提供工作代码或示例?

我至少需要某种绑定或事件,以便当登录状态发生变化时,这些其他感兴趣的组件的 UI 可以相应地更新。

【问题讨论】:

  • addEventListener() 有效吗?
  • 不知道在哪里添加,对不起,你能解释一下吗?

标签: javascript data-binding polymer polymer-1.0 2-way-object-databinding


【解决方案1】:

在您的登录组件中创建一个表示状态的属性并设置notify: true。 在您的登录组件和使用该状态的任何其他组件中使用数据绑定。

<login-component status="{{status}}"></login-component>
<other-component login="{{status}}"></other-component>

如果您在 Polymer 模板之外使用组件,请通过将它们包装在 &lt;template is="dom-bind"&gt; 中来使用自动绑定。

<template is="dom-bind">
    <login-component status="{{status}}"></login-component>
    <other-component login="{{status}}"></other-component>
</template>

【讨论】:

  • 这似乎不起作用。你能给我举一个简单的例子,一个组件可以看到另一个组件的属性吗?
  • 不幸的是,Polymer 文档 (polymer-project.org/1.0/docs/devguide/data-binding.html) 中的绑定部分似乎只讨论了组件内部的绑定,而不是组件之间的绑定。
  • Polymer 模板之外的组件之间的数据绑定在文档的自动绑定部分中进行了描述:polymer-project.org/1.0/docs/devguide/templates.html#dom-bind
  • 我想我现在明白这里发生了什么。通过为 status 属性指定一个变量,您可以让 Polymer 将登录组件的 status 属性绑定到您指定的那个变量。然后,您可以将相同的变量作为输入提供给其他组件。有趣 :) 当然,这些组件必须在同一页面上。
【解决方案2】:

你可以使用&lt;iron-localstorage&gt;as described here

<dom-module id="ls-sample">
  <iron-localstorage name="my-app-storage"
    value="{{cartoon}}"
    on-iron-localstorage-load-empty="initializeDefaultCartoon"
  ></iron-localstorage>
</dom-module>

<script>
  Polymer({
    is: 'ls-sample',
    properties: {
      cartoon: {
        type: Object
      }
    },
    // initializes default if nothing has been stored
    initializeDefaultCartoon: function() {
      this.cartoon = {
        name: "Mickey",
        hasEars: true
      }
    },
    // use path set api to propagate changes to localstorage
    makeModifications: function() {
      this.set('cartoon.name', "Minions");
      this.set('cartoon.hasEars', false);
    }
  });
</script>

【讨论】:

  • 这将带来不幸的副作用,即在页面加载期间保持值,因为 iron-localstorage 是 window.localStorage 的包装器(类似 cookie 的键值存储)。此外,答案根本没有提到它将如何帮助 OP 的问题,但我认为它正在解决这个问题:人们几乎可以像使用全局对象一样使用 iron-localstorage;每个想要访问这个全局的组件都可以有一个 Iron-localstorage,它们都会共享这些数据。
【解决方案3】:

See this Plunker example@nazerke)演示了一个组件观察另一个组件的属性。

http://plnkr.co/edit/p7R8BqJJfoYMVA3t3DbX?p=preview

索引.html
<!DOCTYPE html>
<html lang="en">

<head>
  <script src="http://www.polymer-project.org/1.0/samples/components/webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="parent-element.html">
  <link rel="import" href="first-child.html">
  <link rel="import" href="second-child.html"> </head>

<body>
  <parent-element></parent-element>
</body>

</html>
父元素.html
<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="parent-element">
  <template>
    <first-child prop={{value}}></first-child>
    <second-child feat1={{prop}}></second-child> In parent-element
    <h1>{{value}}</h1> </template>
  <script>
    Polymer({
      is: "parent-element",
      properties: {
        value: {
          type: String
        }
      },
      valueChanged: function() {
        console.log("value changed");
      }
    });
  </script>
</dom-module>
first-child.html
<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="first-child">
  <template>
    <p>first element.</p>
    <h2>{{prop}}</h2> </template>
  <script>
    Polymer({
      is: "first-child",
      properties: {
        prop: {
          type: String,
          notify: true
        }
      },
      ready: function() {
        this.prop = "property";
      }
    });
  </script>
</dom-module>
第二个孩子.html
<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="second-child">
  <template>
    <p>Second element.</p>
    <h2>{{feat1}}</h2> </template>
  <script>
    Polymer({
      is: "second-child",
      properties: {
        feat1: {
          type: String,
          notify: true,
          value: "initial value"
        }
      },
      ready: function() {
        this.addEventListener("feat1-changed", this.myAct);
      },
      myAct: function() {
        console.log("feat1-changed ", this.feat1);
      }
    });
  </script>
</dom-module>

【讨论】:

  • 谢谢,这个例子看起来很有趣,我会尝试通过添加一些事件处理程序来更进一步,这些事件处理程序会更改由用户操作驱动的属性
【解决方案4】:

你可以使用&lt;iron-meta&gt;as described here:

<iron-meta key="info" value="foo/bar"></iron-meta>
...
meta.byKey('info').getAttribute('value').

document.createElement('iron-meta').byKey('info').getAttribute('value');

<template>
  ...
  <iron-meta id="meta"></iron-meta>
  ...
  this.$.meta.byKey('info').getAttribute('value');
  ....
</template>

【讨论】:

  • 如果我没记错的话,这是一种指定可以从页面中的任何位置访问的键/值对的机制?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多