Javascript 如何解决Ionic 3中的“错误:未捕获(承诺):错误:没有提供者”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44580960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:37:44  来源:igfitidea点击:

How to solve "Error: Uncaught (in promise): Error: No provider for" error in Ionic 3

javascriptangularionic3

提问by J Snow

I'm learning Ionic 3 and I'm getting this error when trying to make a custom validator which checks for a unique username. I've tried my best but couldn't solve this issue.

我正在学习 Ionic 3 并且在尝试制作一个检查唯一用户名的自定义验证器时遇到此错误。我已经尽力了,但无法解决这个问题。

CustomValidators.ts

自定义验证器.ts

import { Directive, Input } from '@angular/core';

import { FormControl, Validator, AbstractControl } from '@angular/forms';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Rx';

export class CustomValidators {


    constructor(public http: Http){}

      public hasUniqueEmail(
        control: FormControl,
      ){

        return this.http.get('assets/users.json')
        .map(res => res.json())
        .catch((error:any) => Observable.throw(error.json().error || 'Server error'));

      }


}

signup.ts

注册.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { FormBuilder, FormGroup,  Validators } from '@angular/forms';
import { CustomValidators } from '../../components/CustomValidators';

/**
 * Generated class for the SignupPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */


@IonicPage()

@Component({
  selector: 'page-signup',
  templateUrl: 'signup.html',
})


export class SignupPage {

  private sform : FormGroup;

  constructor(
        private formBuilder: FormBuilder,
        private myValidator: CustomValidators,
    ){

    this.sform = this.formBuilder.group({
      name: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])],
      email:  ['', Validators.compose([Validators.email,this.myValidator.hasUniqueEmail])],
      password: ['',Validators.required                                                                                             ],
    });

  }


  logForm() {

  }


}

This is the error that I'm getting:

这是我得到的错误:

"Uncaught (in promise): Error: No provider for CustomValidators!
Error
    at Error (native)
    at injectionError (http://localhost:8100/build/main.js:1583:86)
    at noProviderError (http://localhost:8100/build/main.js:1621:12)
    at ReflectiveInjector_._throwOrNull (http://localhost:8100/build/main.js:3122:19)
    at ReflectiveInjector_._getByKeyDefault (http://localhost:8100/build/main.js:3161:25)
    at ReflectiveInjector_._getByKey (http://localhost:8100/build/main.js:3093:25)
    at ReflectiveInjector_.get (http://localhost:8100/build/main.js:2962:21)
    at NgModuleInjector.get (http://localhost:8100/build/main.js:3909:52)
    at resolveDep (http://localhost:8100/build/main.js:11369:45)
    at createClass (http://localhost:8100/build/main.js:11225:91)"

回答by Sajeetharan

You need to add the provider to the NgModule, i.e module.ts under providers,

需要在NgModule中添加provider,即provider下的module.ts,

providers: [
  CustomValidators 
]

回答by Baruch

From what I can see you're missing 2 things

据我所知,你错过了两件事

1) no decorator for the class, you're importing Directivebut never using it

1)该类没有装饰器,您正在导入Directive但从未使用它

import { Directive } from '@angular/core';

@Directive({
  name: 'directive-name'
})
export class CustomValidators {
  //...
}

2) there's no import in the NgModule

2) NgModule 中没有导入

import { CustomValidators } from 'path/to/file';

@NgModule({
  //...
  providers: [
    CustomValidators
  ]
})