首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >聚合物与宿主的数据绑定

聚合物与宿主的数据绑定
EN

Stack Overflow用户
提问于 2016-10-17 23:08:33
回答 1查看 140关注 0票数 0

我在弄清楚聚合物数据绑定到一个元素主机是如何工作的有问题。我想要改变选定的页面的<neon-animated-pages>元素,取决于用户是否登录,但我是完全陌生的聚合物,不知道如何使用数据绑定。我甚至不能让他们用javascript改变元素.

任何帮助都将不胜感激!

我的主要元素如下所示:

代码语言:javascript
复制
<template>
  <neon-animated-pages id="animated_pages" selected='{{display}}' entry-animation='slide-from-left-animation' exit-animation='slide-right-animation'>
    <neon-animatable><login-page loggedIn={{loggedIn}} user={{user}}></login-page></neon-animatable>
    <neon-animatable><main-page></main-page></neon-animatable>
    <neon-animatable>Baz Page, the Best One of the Three</neon-animatable>
  </neon-animated-pages>
</template>

Polymer({
  is: 'app-page',

  properties: {
    signedIn: {
      type: Boolean,
      notify: true,
      value: false,
      observer: 'showMain'
    },
    user: {
      type: Object,
      notify: true,
    }
  },
  showMain: function(){
    this.displayed = 1;    <--doesnt seem to work!?!
});

我的<login-page>元素如下所示:

代码语言:javascript
复制
<template>
  <firebase-auth
     id="auth"
     app-name="MyApp"
     provider="password"
     signed-in="{{signedIn}}"
     user="{{user}}">
  </firebase-auth>

  <form is="iron-form" id="login_form" class="bottom">
    <div>{{signedIn}}</div>
    <paper-button id="login_button" on-tap="logIn" raised>Log in</paper-button>
    <paper-input id="login_password" name="password" label="password" type="password" on-keyup="_handleEnter" required></paper-input>
    <paper-input id="login_email" ></paper-input>
  </form>
<template>

<script>
  Polymer({

    is: 'login-page',

    properties: {
      signedIn: {
        type: Boolean,
        notify: true,
      },
      user: {
        type: Object,
        notify: true,
      }
    },
  logIn: function() {
      var email = this.$.login_email.value
      var password = this.$.login_password.value
      firebase.auth().signInWithEmailAndPassword(email, password)
      .then(function(response) {
        this.signedIn = true;    <--doesn't seem to work!?!
        this.user = response;
      })
      .catch(function(error) {
        console.log(error)
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        alert(errorMessage);
        this.signedIn = false;
      }.bind(this));
    },
  });
</script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-18 02:48:29

看起来您在绑定中使用了错误的属性名称:

代码语言:javascript
复制
<login-page loggedIn={{loggedIn}}></login-page>

注意,<login-page>没有一个名为"loggedIn“的属性。您可能打算使用"signedIn“。

还请注意,属性绑定使用属性形式的属性名(破折号不是camelCase) (参见关于数据绑定的文档)。要绑定"signedIn",您可以使用:

代码语言:javascript
复制
<login-page signed-in="{{loggedIn}}"></login-page>

最后,您只在this回调中绑定了catch,而没有绑定用于then的回调,因此thenthis并不是指您的聚合物对象。修正:

代码语言:javascript
复制
Polymer({
  ...
  logIn: function() {
    ...
    firebase.auth().signInWithEmailAndPassword(email, password)
      .then(function(response) {
        this.signedIn = true;
        this.user = response;
      }.bind(this)) // bind `this` to the Polymer object
      .catch(function(error) {
        ...
      }.bind(this));
  })
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40097038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档