SB-Admin-BS4-Angular-4,增加服务

时间:2017-08-17 11:45:37

标签: angular

我一直收到错误

  

错误:模块'SignupModule'声明的意外值'SignupService'。请添加@ Pipe / @ Directive / @ Component注释。

虽然我已经注入了对SignupModule的所有依赖 这是我的代码

//SignupModule 
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

import { SignupRoutingModule } from './signup-routing.module';
import { SignupComponent } from './signup.component';

import {SignupService} from './signup.component.service';


@NgModule({
 imports: [
   CommonModule,
   FormsModule,
   SignupRoutingModule
],
declarations: [
  SignupComponent,
   SignupService]
})
export class SignupModule { }
  

这里是signup.component

import { Component, OnInit } from '@angular/core';
import { routerTransition } from '../router.animations';

import { SignupService } from './signup.component.service';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.scss'],
  animations: [routerTransition()],
  providers:[ SignupService]
})

export class SignupComponent implements OnInit {
 user:user;

  constructor(signupService:SignupService) {
    console.log(signupService.register());
    this.user={
        firstName:'',
        middleName:'',
        lastName:'',
        gender:'',
        email:'',
        password:'',
        repeatPassword:''
    }
 }

ngOnInit() { }

signup(){
    console.log(this.user);
 }
}


interface user{
  firstName:string,
  middleName:string,
  lastName:string,
  gender:string,
  email:string,
  password:string,
  repeatPassword:string
}
  

这里是signup.component.service

export class SignupService{
register(){
    console.log('service');
    return 'test';
  }
}

1 个答案:

答案 0 :(得分:0)

必须提供未声明的服务。从app.module.ts的声明部分移除 SignupService 。它应该可以解决问题。