2

我有一个可以使用 firebase 登录的应用程序。我为此使用了polymerfire。有一个主要的应用程序元素:my-app有这些孩子:

<firebase-app
    auth-domain="my-app-ID.firebaseapp.com"
    database-url="https://my-app-ID.firebaseio.com"
    api-key="my-app-apikey">
</firebase-app>
<firebase-auth
    id="auth"
    user="{{user}}"
    status-known="{{statusKnown}}"
    provider="google"
    on-error="handleError">
</firebase-auth>

<iron-pages selected="{{page}}" attr-for-selected="name">
    <my-loading name="loading"></my-loading>
    <my-home name="home"></my-home>
    <my-dashboard name="dashboard"></my-dashboard>
</iron-pages>

page随元素的statusKnownanduser属性而变化。firebase-auth虽然statusKnown是页面是可见false的。loadingstatusKnownis时,true它选择任home一屏幕,当userisnull或 users时dashboard

  _signInChanged(statusKnown) {
      if(this.statusKnown) {
          if(this.user) {
              this.page = "dashboard"
          } else {
              this.page = "home"
          }
      } else {
          this.page = "loading"
      }
  }

屏幕home上有一个登录按钮,然后单击它必须调用该firebase-auth函数signInWithPopup()。但是firebase-auth无法从元素访问该my-home元素。它不能通过属性找到document.getElementByID或作为属性传递。或者以某种方式访问​​ if 使用父指针this.parentElement.$.auth.signInWithPopup()

我怎样才能auth从孩子那里得到元素?

4

1 回答 1

2

你可以用不同的方式来做。多亏了事件和侦听器,您将能够将一些事件传播给父级,然后在父级元素中使用this.$.auth.

所以:

my-home元素处,当您准备好调用 firebase auth 时,只需调用类似的东西:this.fire("logIn");并在函数内部的 parent-elemet 中ready写下:

this.addEventListener("logIn", function() {
  this.$.auth.signInWithPopup();
}.bind(this));

就是这样。

于 2017-05-22T10:51:48.143 回答