2

I am trying to extend some components with another class but I got

[Angular] Can't resolve all parameters for MyProjectsComponent in 'pathtomyproject'/src/app/my-projects/my-projects.component.ts: ([object Object], ?).

security.ts

import { Router} from '@angular/router';

export abstract class Security {

  constructor(
    protected router: Router
   ) { }

  isLogged() {
    if (!sessionStorage.getItem('user')) {
      this.router.navigate(['/login']);
    }
  }
}

my-projects.component

import { Component, OnInit } from '@angular/core';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { ProjectService } from './project.service';
import { Security } from '../security';

@Component({
  selector: 'app-my-projects',
  templateUrl: './my-projects.component.html',
  styleUrls: ['./my-projects.component.scss'],
  providers: [ProjectService],
})
export class MyProjectsComponent extends Security implements OnInit {

  projects: Array<any>;

  constructor(
    private projectService: ProjectService,
    protected router
  ) {
      super(router);
    }

  ngOnInit() {
    this.getProjects();
  }

  async getProjects() {
    const data: any = await this.projectService.getAll();
    this.projects = data._embedded.projects;
  }

  delete(id) {
    this.projectService.delete(id);
    this.getProjects();
  }

  edit(id) {
    const path = `/edit/${id}`;
    this.router.navigate([path]);
  }

}
4
  • 1
    Can you show your app.module.ts? RouterModule.forRoot() should be imported by the AppModule. If you import it in a feature module or a shared module that's lazy loaded, you could run into issues. Same is true if you've imported .forRoot multiple times. Commented Dec 19, 2017 at 9:38
  • Another possibility - check to make sure that @angular/router is installed - some simple things to rule out. Commented Dec 19, 2017 at 9:43
  • As I understand is that Angular cannot resolve the router. Can you try this: inject the Router like this public router: Router in the MyProjectsComponent constructor Commented Dec 19, 2017 at 9:49
  • @pixelbits yeah that was it, and I also changed the injection to router: Router as Daniomi wrote Commented Dec 19, 2017 at 10:24

1 Answer 1

1

There might be some issue in the constructor of the MyProjectsComponent. You misconfigured the dependency injection of the router. Change it to:

constructor(
  private projectService: ProjectService,
  protected router: Router // <-- DI for Router
) {
  super(router);
}

and then it should work.

I tried to reproduce your issue in this stackblitz:

https://stackblitz.com/edit/angular-aifdew

Sign up to request clarification or add additional context in comments.

1 Comment

It helped, I also needed to add RouterModule into my module file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.