The class is not correctly implementing the 'OnInit' interface. The 'ngOnInit' property is missing in the 'Component' type, which is required in the 'OnInit' type

Could someone offer assistance with this issue? It seems like there are errors in the code structure:

Class 'ContactFormComponent' incorrectly implements interface 'OnInit'. Property 'ngOnInit' is missing in type 'ContactFormComponent' but required in type 'OnInit'

'ngOnInit' is declared here.

and

Cannot find name 'ngOnInit'

import { Component, Injectable, OnInit } from '@angular/core';
import { FormBuilder} from '@angular/forms';
import { Validators} from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { HttpErrorResponse } from '@angular/common/http';
import 'validator';

@Component({
  selector: 'app-contact-form',
  templateUrl: './contact-form.component.html',
  styleUrls: ['./contact-form.component.scss'],
})

export class ContactFormComponent implements OnInit {

      
  checkoutForm = this.fb.group ({
    firstName :  ['', Validators.required, {updateOn: 'blur'}],
    lastName :  ['', Validators.required, {updateOn: 'blur'}],
    email :  ['', Validators.required, {updateOn: 'blur'}],
    password :  ['', Validators.required, {updateOn: 'blur'}],
    address : this.fb.group ({
      state :  ['', Validators.required, {updateOn: 'blur'}],
      country :  ['', Validators.required, {updateOn: 'blur'}],
      city :  ['', Validators.required, {updateOn: 'blur'}],
      zip :  ['', Validators.required, Validators.minLength(6), Validators.maxLength(6), {updateOn: 'blur'}],
    })
  });

  constructor(private fb: FormBuilder, private httpService: HttpClient) { }

  get form()
  {
      return this.checkoutForm.controls;
  }

  get firstName(){
    return this.checkoutForm.get('firstName');

  }
  get lastName(){
    return this.checkoutForm.get('lastName')
  }
  get email(){
    return this.checkoutForm.get('email')
  }
  get password(){
    return this.checkoutForm.get('password')
  }
  get state(){
    return this.checkoutForm.get('state')
  }
  get country(){
    return this.checkoutForm.get('country')
  }
  get street(){
    return this.checkoutForm.get('street')
  }
  get zip(){
    return this.checkoutForm.get('zip')
  }

  onSubmit(){
    console.warn(this.checkoutForm.value)
  }

}

ngOnInit() {
  //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
  //Add 'implements OnInit' to the class.

}

Answer №1

Realized my mistake.

ngOnInit() {
//Occurs after the constructor, sets up input properties, and is the initial call to ngOnChanges.
  //Ensure the class implements OnInit.
}

The code above should have actually been placed as shown below

export class ContactFormComponent implements OnInit { }

for instance

export class ContactFormComponent implements OnInit {
  ngOnInit() {
  //Occurs after the constructor, sets up input properties, and is the initial call to ngOnChanges.
  //Ensure the class implements OnInit.
  }
}

Answer №2

Place "ng" before OnInit() as shown below:

  ngOnInit(): void {
      // console.log('hello');
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular rxjs Distinctions

Coming from AngularJS to Angular, I'm still trying to wrap my head around rxjs observable. For example: User.ts export class User { id?:any; username:string; password:string; } Using <User[]> myUser(header: any) { const url = `${this.mainUr ...

What is the best way to utilize "exports" in package.json for TypeScript and nested submodules?

Looking to leverage the relatively new "exports" functionality in Node.js/package.json for the following setup: "exports": { ".": "./dist/index.js", "./foo": "./dist/path/to/foo.js" } so that ...

Retrieve server information without utilizing data.map since array.map is not a supported function in next.js version 13

Currently, I am developing a list page using next.js 13 to display a collection of projects. I made an attempt to enable server-side rendering for this feature. The implementation is located in app/team/page.tsx import { useRouter } from 'next/navig ...

Encountering a problem with a fresh Ionic starter project - the tabs functionality causes the app to crash on both the iOS simulator and device. However, it

After setting up an Ionic tab project on my Mac, I encountered a problem when trying to run the app on the iOS simulator and device. The app crashes immediately after opening, without showing any error messages. Interestingly, the app works perfectly fine ...

What is the purpose of `{ _?:never }` in programming?

I've been going through some TypeScript code and I stumbled upon a question. In the following snippet: type LiteralUnion<T extends U, U extends Primitive> = | T | (U & { _?: never }); Can anyone explain what LiteralUnion does and clarif ...

During the evaluation of an Angular application, an examination was conducted on the feature where clicking a button would increase a count and show the result in an h2 element

After clicking a button, the count is supposed to increase and the updated count should be displayed on the HTML page using Angular. I have written a test for this scenario using Jasmine + Karma runner, but the expected value is not matching. During testi ...

Using only HTML, I have created a collapsible accordion in Angular without the need for JS code. However, when I click the button, nothing happens. Can you help me resolve

I am attempting to add an Accordion button to my website using the HTML code provided below. I am currently incorporating this in a very basic manner, without any utilization of javascript code, within this example of accordion snippet. When the button i ...

Upgrading the import package alias from 'angular2' to '@angular'

Is there a way to update the package alias for import purposes? I am trying to utilize ng2-bootstrap, which requires @angular/core, but I have been referencing it as angular2/core. This mismatch is causing my screen to crash! ...

Optimizing Angular 2's Efficiency: A Deep Dive into Change Detection and Performance Metrics (1000 calls measured

As a newcomer to Angular 2, I recently inherited a project using the framework. Upon assessing the app's performance, I noticed it was running quite slowly and causing issues. I investigated further and discovered that many ngIf's and ngFor&apo ...

404 error received from Angular 2 API call despite successful testing of service

I've been attempting to make a web service call from my Angular 2 application. private baseUrl: string = 'http://localhost:3000/api'; getPatterns(): Observable<Pattern[]> { const patterns$ = this.http .get(`${this.baseUrl ...

Creating a form with multiple components in Angular 6: A step-by-step guide

I'm currently working on building a Reactive Form that spans across multiple components. Here's an example of what I have: <form [formGroup]="myForm" (ngSubmit)="onSubmitted()"> <app-names></app-names> <app-address> ...

JavaScript - untimely initiation of await functionality

I'm a beginner when it comes to using async and await, and I could really use some assistance. In my code, there's a function called register which registers a user and sends their data to the server to create a "user profile". However, I'm ...

What is the best method to eliminate a "0" entry from a javascript event array?

Hello, I've got an array structured as follows: let test = ["testOne:,O,U,0","testTwo:R,C,0","testTree:1.334","testFour:r,z"]; I'm looking to iterate through the array and remove any occurrences of the cha ...

The Gulp task is stuck in an endless cycle

I've set up a gulp task to copy all HTML files from a source folder to a destination folder. HTML Gulp Task var gulp = require('gulp'); module.exports = function() { return gulp.src('./client2/angularts/**/*.html') .pipe( ...

Is it time to refresh the package-lock.json file?

I'm currently in the process of updating a React app from Node 14 to 16. One step I took during the upgrade was deleting the node_modules folder and package lock, then generating a new package-lock.json file. However, this resulted in numerous compila ...

Troubleshooting Angular's CORS problem by querying an endpoint at 5-second intervals

My angular application is set up to query a node/express backend endpoint every 5 seconds, but I am encountering occasional CORS errors. Scenario A user initiates a transaction, which is then added to the database with a "Pending" status The frontend co ...

Avoid the import of @types definition without exports in TypeScript to prevent the error TS2306 (not a module)

I have spent a considerable amount of time trying to load a NodeJS library that has what I believe is a faulty type definition in the @types repository. The library in question is geolib and its types can be found in @types/geolib Although I am aware tha ...

The Angular directive ng-if does not function properly when trying to evaluate if array[0] is equal to the string value 'Value'

In my code, I want to ensure that the icon is only visible if the value at array index 0 is equal to 'Value': HTML <ion-icon *ngIf="allFamily[0] === 'Value'" class="checkas" name="checkmark"></ion-icon> TS allFamily = [ ...

send the user to a different HTML page within an Angular application

Does anyone have the answer to my question? I need help figuring out where the comment is located that will redirect me to another page if it's false. <ng-container *ngIf="!loginService.verificarToken(); else postLogin"> <ul clas ...

How can I pass a service method as a parameter in an Angular 2 component?

Within the component: myFunction(): void { this.myOtherFunctoin(this._myService.serviceMethod); } private myOtherFunction(func : Function){ func(); } Regarding service calls: serviceMethod(){ this.somethingMethod(); // "this" is coming as ...