2021年2月7日星期日

Unable to use more than one service in component

I'm just getting started with learning angular and web development in general. I'm trying to use the angular router to redirect after the form is submitted. But I'm getting an error on the second service that its not declared in the component. If I just have one it works Code:

import { Component, OnInit } from '@angular/core';  import { HttpClient } from '@angular/common/http';  import { Router } from '@angular/router';    @Component({    selector: 'app-question',    templateUrl: './question.component.html',    styleUrls: ['./question.component.css']  })  export class QuestionComponent implements OnInit {        constructor(private http: HttpClient, router: Router) { }    onCreatePost(postData: { title: string; email: string; content: string }) {      // Send Http request      console.log(postData);      this.http.post(' https://myapi/posts.json',postData)      .subscribe();      this.router.navigate(['/home']);     }    ngOnInit(): void {    }    }    

error: Property 'router' does not exist on type 'QuestionComponent'.

https://stackoverflow.com/questions/66095241/unable-to-use-more-than-one-service-in-component February 08, 2021 at 10:35AM

没有评论:

发表评论