简体   繁体   English

Angular2,组件未显示

[英]Angular2, Component not showing

This is my component, error-handler.ts 这是我的组件error-handler.ts

@Component({
  selector: 'error-handler',
  templateUrl: 'error-handler.html'
})
export class ErrorHandlerComponent {

  @Input()
  error: string;

  constructor() {}

  ngOnChanges() {}
}

This is my error-handler.html: 这是我的error-handler.html:

<div>
    <span> {{error}} </span>
</div>

This is how I'm using it in login.html 这就是我在login.html中使用它的方式

<error-handler class="smallWidth" [error]=error></error-handler>

error-handler shows that error is received in the logs, but I still don't see my error being displayed on the page. 错误处理程序显示日志中接收到错误,但是我仍然看不到该错误显示在页面上。 On the login page: 在登录页面上:

if (this.platform.is('core')) {
    this.error = "ERROR";
    this.provider = new firebase.auth.GoogleAuthProvider();
    this.afAuth.auth.signInWithPopup(this.provider)
    .then((currentUser) => {
    })
}

So when I click login button which calls my above method, I can see my error div show up. 因此,当我单击调用上述方法的登录按钮时,可以看到我的错误div出现了。 The below code is also running from my app.component.ts 下面的代码也在我的app.component.ts中运行

  this.http.post(this.urlEnvironment.getUser(), objectToSend)
    .catch((error: any) => {
      this.error = "ERROR";
    })
    .subscribe((data: Response) => {
    )};

From the above I see the code going into the error-handler.ts and printing to the logs, but the error div doesn't get displayed. 从上面我可以看到代码进入了error-handler.ts并打印到日志中,但是没有显示错误div。 Above is hitting my local server which is turned off so I see that the code does go into the catch block and sets the this.error to ERROR. 上面是我关闭的本地服务器,所以我看到代码确实进入了catch块,并将this.error设置为ERROR。

UPDATE TO NEW QUESTION ABOUT FIREBASE AUTH: 有关FIREBASE AUTH的新问题的更新:

If you need to trigger error message in reaction to Firebase Auth, then you rather create UserService and ModalService. 如果您需要在响应Firebase Auth时触发错误消息,则可以创建UserService和ModalService。 In Auth promises handled by UserService trigger the modal when error occurs. 在UserService处理的Auth Promise中,发生错误时会触发模式。

For example your UserService may look like this: 例如,您的UserService可能如下所示:

 // User service using email login import { Injectable } from '@angular/core'; import { AngularFireAuth } from 'angularfire2/auth'; import * as firebase from 'firebase'; import { ModalService } from '@services/modal.service'; @Injectable() export class UserService { constructor( private _fireAuth: AngularFireAuth, private _modal: ModalService ) {} login = ( email, password ) => { this._fireAuth.auth.signInWithEmailAndPassword( email, password ) .then( () => ... do something with the user ) .catch( ( error ) => this._modal.show( err ) ); }; } 

It really depends what you need to do with the error message (ie. should there be a button to click and trigger action then ... or if you need custom error messages ...) to say how the Modal Service / Component should look like. 这实际上取决于您需要如何处理错误消息(即是否应该有一个按钮来单击并触发操作,然后……或者您是否需要自定义错误消息……),以说明模态服务/组件的外观喜欢。


ORIGINAL ANSWER TO DATA BINDING: try 数据绑定的原始答案:尝试

<error-handler [error]=error></error-handler>

if that does not help confront your app data and logic with docs on angular data binding 如果那不能帮助您通过角度数据绑定文档来应对您的应用数据和逻辑

But looking into your example, it seems you really do not need much of inner elements for that error, you can probably reduce it into something like 但是看一下您的示例,看来您确实不需要太多内部元素来处理该错误,您可以将其简化为

<error-handler [innerHTML]=error_message></error-handler>

and just style it using :host selector in you css ... 然后在您的CSS中使用:host选择器对其进行样式设置...

better you develope component like this: 更好地开发这样的组件:

<div>
    <span><ng-content></ng-content></span>
</div>

and then use like this: 然后像这样使用:

<error-handler>this place is your error message</error-handler>

or 要么

<error-handler>{{error}}</error-handler>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM