0

I want to call a service imported in my @ngModule from a login component. This what i did but it's not working. I have just started to work on the A2 final version.

export class Login implements OnInit{
  constructor(
    private _service: Service
    ) {
  }
  ngOnInit() {
    this._service.login(value)
        .subscribe(x => console.log(x));
    }
  }
}

My @ngModule

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { LoginService } from '../service/login.service'; 

import { Login } from './login.component';

export const routes = [
  { path: '', component: Login, pathMatch: 'full' }
];

@NgModule({
  declarations: [
    Login
  ],
  imports: [
    CommonModule,
    RouterModule.forChild(routes)
  ],
  providers: [LoginService]
})
export default class LoginModule {
  static routes = routes;
}

Any ideas ?

1

3 Answers 3

1

Inside constructor Service should be LoginService, as you wanted to access/create an instance of LoginService. Also make sure LoginService has been imported.

constructor(
   private _service: LoginService //<-- changed dependency type name
   ) {
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get this : Error: Can't resolve all parameters for Login: (Router, ?).
@AdrienCastagliola do you have router-outlet directive on page..?
1

Chage your component class to use LoginService

export class Login implements OnInit{
  constructor(
    private _service: LoginService
    ) {
  }
  ngOnInit() {
    this._service.login(value)
        .subscribe(x => console.log(x));
    }
  }
}

Comments

0

You have to import LoginService in Login component also and add it to constructor as:

constructor( _service: LoginService) {  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.