Inheriting Components in Angular 2.3

Trying to consolidate common code that adds server errors to form components is my current challenge. I want this code to be reusable across all my forms within a base form component.

To illustrate my issue, here is a simplified version of the code snippet:

import { Component } from '@angular/core';
import { Validators } from '@angular/forms';
import { FormGroup, FormBuilder } from '@angular/forms';

export abstract class BaseFormComponent {
  form: FormGroup;
  fb = new FormBuilder;
  submitted = false;
  busy: boolean;

  onSubmit() {
    this.submitted = true;
    this.busy = true;
  }
}

@Component({
  selector: 'my-form',
  template: `
    <form [formGroup]="form" (ngSubmit)="onSubmit()" novalidate>
      <div class="form-group">
        <label>Name</label>
        <input formControlName="name" class="form-control">
      </div>
      <button type="submit" [disabled]="busy" class="btn btn-primary">Submit</button>
    </form>
  `
})
export class FormComponent extends BaseFormComponent {

  constructor() {
    super();
    this.createForm();
  }

  protected createForm() {
    this.form = this.fb.group({
      name: ['', Validators.compose([Validators.required, Validators.maxLength(10)])],
    });
  }
}

The main problem I am facing now is encountering errors indicating that the template cannot find properties from the base class.

Error: [21, 24]: The property "form" you are trying to access does not exist in the class declaration. [21, 42]: The method "onSubmit" you are trying to access does not exist in the class declaration. [26, 41]: The property "busy" you are trying to access does not exist in the class declaration.

Upon comparing my code to this article, it appears quite similar, although I have not marked the base form class as a component. However, even when doing so, there seems to be no effect.

The tools and libraries I am using include:

Answer №1

In response to my own inquiry, it turns out that the code itself is not the problem; rather, the issue lies with Codelyzer. More information about the issue can be found here.

A temporary solution to this problem is to simply disregard it for now by adding the following comment at the beginning of the file, until any conflicts between tslint and codelyzer are resolved:

// tslint:disable:no-access-missing-member

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

What does the "xxx" parameter represent in the ng g universal xxx command when using AngularCLI 6+?

In this scenario, what is the purpose of morningharwood-server? Can we find it referenced in the code? ng generate universal morningharwood-server --client-project morningharwood ...

Creating a Two-Way Binding Input Field in Angular to Display 'Invalid Date' Message if Incorrect Date is Selected in Datepicker

I am using an input field with an angular datepicker that is two-way bound to my model, which is of type string. The datepicker formats the existing date in DD/MM/YYYY format after converting it to toISOString(). However, if a non-existent date is entere ...

A guide to styling rows individually in an ngFor loop with Angular 5

I am currently developing a table that populates rows using the *ngFor directive: <tr *ngFor="let car of cars"> <td>{{car?.id}}</td> <td>{{car?.date | date : "short"}}</td> <div [ngSwitch]="car?.state ...

It is not always a guarantee that all promises in typescript will be resolved completely

I have a requirement in my code to update the model data { "customerCode": "CUS15168", "customerName": "Adam Jenie", "customerType": "Cash", "printPackingSlip": "true", "contacts": [ { "firstName": "Hunt", "lastName": "Barlow", ...

The CSS Loader appears to be failing to export any content

I have a Node.js application built with TypeScript, ReactJS, webpack, and CSS loader. After importing CSS modules like this: import * as myStyles from './css/myCustomStyles.css'; The bundling process is successful, and the output appears to be ...

Selecting string properties in TypeScript

Trying to solve the puzzle of accurately coding a show function that requires an object T and a key K where T[K] definitely has a method named toString(). Here's my approach using mapped types type ToStringablePropertyKeys<T> = keyof { [K i ...

Adjust the setting for the useHash parameter within the RouterModule during execution

I am faced with a situation where I need to dynamically load my router module option useHash in my Angular application, sometimes with true and other times with false. This decision depends on the server state data that is available in the global window ob ...

Providing a function with an incorrect variable type does not trigger a type error

Why is it that when I pass the incorrect variable type to the ts function, no type error is emitted? export class CreateCategoryDto implements Omit<Category, 'id' | 'children' | 'parent'> { name: string; parentId: ...

Issue with Angular, FormControl not binding correctly to formControlName in the form

When manually creating a form model using FormGroup & FormControl, everything seems fine until angular binds the FormControl to the corresponding input and I get an unexpected result. The model is created and bound to the HTML in the following way: ...

Angular Ahead-of-Time (AOT) compilation causes multiple route definitions to be

Having a bit of trouble configuring ahead-of-time compilation for my lazy-loaded Angular app. The lazy-loaded routes are specified in the app.routes.ts file, which is imported by app.module.ts. Running ngc results in the content of app.routes.ts being mer ...

Using Angular and Typescript to implement a switch case based on specific values

I am attempting to create a switch statement with two values. switch ({'a': val_a,'b': val_b}){ case ({'x','y'}): "some code here" break; } However, this approach is not functioning as expected. ...

Looking to seamlessly integrate a CommonJS library into your ES Module project while maintaining TypeScript compatibility?

I am interested in creating a project with Typescript. The project is built on top of the Typescript compiler, so I am utilizing Typescript as a library, which I believe is a CommonJS library. Although the project is designed to run on Node (not in the bro ...

Unlocking the Power of Custom Validators in Angular Reactive Forms

I have created a personalized validator as shown below import { Directive, forwardRef } from '@angular/core'; import { NG_VALIDATORS, FormControl } from '@angular/forms'; function customValidationFactory(goalAmount: number, quantity: ...

What is the process for including a dependency in an npm package in order to install another npm package?

As I work on creating an npm package for my Angular app, I find myself in need of including a dependency that already exists on npm. My dependency object will include the following entries: "dependencies": { "@angular/animations": "^6.1.0", "@angu ...

Error during module compilation: package @babelcore could not be located in the babelindex.js file

I've been grappling with this issue for days now. Despite my research efforts, I can't seem to resolve it. Whenever I run "ng serve" in the terminal, I keep getting the following error: > ./node_modules/@angular/router/fesm2020/router.mjs - Er ...

What could be causing the sudden appearance of this error message: File C:UsersdAppDataRoaming pm g.ps1 cannot be loaded?

For years, I've been happily using vscode/angular/node/npm without any issues. However, in the last hour or so, I've encountered an error without making any explicit changes. What could have caused the execute permissions, which were previously ...

Tips for assigning a value to a data property within a different Vue component

I am faced with a situation where I have two Vue components. The first one triggers the opening of a modal, while the second one serves as the content within that modal in the form of a table and a brief form. Upon completing the form, my goal is to click ...

Tips on filtering an array in a JSON response based on certain conditions in Angular 7

Looking to extract a specific array from a JSON response based on mismatched dataIDs and parentDataIDs using TypeScript in Angular 7. { "data":[ { "dataId":"Atlanta", "parentDataId":"America" }, { "dataId":"Newyork", ...

What is the best way to verify observables during the ngOnInit phase of the component lifecycle?

Despite reading numerous articles on testing observables, such as learning how to write marble tests, I am still struggling to find a clear solution for testing a simple observable in the ngOnInit lifecycle of my component. ngOnInit(): void { this.sele ...

Enhancing HTML element appearance in TypeScript and Angular: A guide to adding style

I have been attempting to apply the flex-grow property to an element, but it does not seem to be taking effect. Here is the script I am using: (this.elementRef.nativeElement as HTMLElement).setAttribute( 'style', 'flex-grow:&apo ...