Angular2 错误 - 我的登录组件没有提供程序错误

Posted

技术标签:

【中文标题】Angular2 错误 - 我的登录组件没有提供程序错误【英文标题】:Angular2 error - No provider error for my login component 【发布时间】:2016-03-16 10:18:19 【问题描述】:

Angular2 没有提供者错误。

来自浏览器控制台的确切错误消息:“例外:用户没有提供者!(登录 -> 用户)”。

打字稿编译器编译良好。

模板加载正常。

但是浏览器控制台有这个错误。

另外,你能用 click 和 ng-model 检查我的模板是否正确吗?

这是我的代码:

 import Component, View, Injectable, CORE_DIRECTIVES, FORM_DIRECTIVES from 'angular2/angular2';
 import AuthService from '../../authService';

 @Injectable()
 class User
    email:string;
    password: string;
  

 @Component(
   selector: 'Login',  
 )

 @View(
   templateUrl: '/src/app/components/login/login.html',
   directives: [CORE_DIRECTIVES, FORM_DIRECTIVES],
 )

 @Injectable()
 export class Login 
   authService:AuthService;
   user: User;

   constructor(authService: AuthService, user: User)   
       this.authService = authService;
       this.user = user;
   

   login = () => 
      console.log("login");
   
 
enter code here

这是我的这个组件的模板:

  <section class="container centered">
      <div class="row">
          <div class="well bs-component col-xs-12 col-xs-offset-0 col-sm-12 col-sm-offset-0 col-md-6 col-md-offset-3 col-lg-offset-4 col-lg-4">
              <h3>Log In</h3>

            <form class="padding-top-20" role="form" novalidate> 
                  <div class="form-group">
                      <input class="form-control" type="text" name="email" id="email" placeholder="Email Address" [(ng-model)]="user.userName" required autofocus />
                  </div>

                  <div class="form-group">
                      <input class="form-control" type="password" name="password" id="password" placeholder="Password" [(ng-model)]="user.password" required autofocus />
                  </div>

                  <button class="btn btn-primary btn-wide" type="submit" (click)="login()">Log In</button>

                  <div class="padding-top-20 padding-bottom-20">
                      <a href="/forgotpassword" title="Forgot Password?">Forgot Password?</a>
                  </div>
              </form>
          </div>
      </div>
  </section>

这是浏览器控制台错误的截图:

【问题讨论】:

您是否关注过这个问题:***.com/questions/30238057/… ? 是的,我看过,但打字稿编译器不喜欢注射剂。 我搞定了。我不得不将其更改为提供者而不是注射剂。 是的;我刚刚在这里读到:***.com/questions/30580083/… 【参考方案1】:

要注册注射剂,在你的组件定义中,使用:

@Component(
   selector: 'Login',  
   providers: [Login, User]
)

【讨论】:

以上是关于Angular2 错误 - 我的登录组件没有提供程序错误的主要内容,如果未能解决你的问题,请参考以下文章

Angular 2没有提供者错误

使用 Jasmine 间谍进行 Angular 2 组件测试“没有 Http 提供者!”错误

Angular2 RC5 ngModule:没有 NameService 的提供者

Angular 2 - 独立组件(无布局)

如果我的导入语句中没有 .js,Angular 2 会抛出错误

从服务传递到组件的角度错误消息