The type 'angular' does not have a property of this kind

Having trouble importing a method into my Angular component.

An error keeps popping up:

Property 'alerta' does not exist on type 'typeof PasswordResetService'. any

I've double-checked the code and everything seems to be in order!

The issue arises in the password-reset.component.ts file where I'm trying to call that method.

import { PasswordResetService } from './password-reset.service';

@Component({
  selector: 'app-password-reset',
  templateUrl: './password-reset.component.html',
  styleUrls: ['./password-reset.component.css']
})
export class PasswordResetComponent implements OnInit {
  modalRef: BsModalRef; 
  constructor(private modalService: BsModalService) { }

  openAlertModal() {
    PasswordResetService.alerta("Title", "Message").subscribe((answer) => {});
  }

The problem lies within the openAlertModal() method...

If you review the PasswordResetService, everything appears to be fine:

export class PasswordResetService {
  BsModalRef: BsModalRef;

  constructor(private BsModalService: BsModalService) { }

  public alerta(title: string, message: string) : Observable<string> {
    const initialState = {
        title,
        message,
    };
    this.BsModalRef = this.BsModalService.show(AlertModalComponent, { initialState });
  
    return new Observable<string>(this.getAlertSubscriber());
  }

The alerta() method is definitely there!

Can anyone offer assistance with this?

Thank you!.

Answer №1

If you're looking to utilize a service, make sure to include it in the constructor. documentation

  1. First, register the service in the module (app.module.ts)
import { PasswordResetService } from './password-reset.service'; // Specify the correct path

@NgModule({
  declarations: [
     // Your Components
  ],
  providers: [
     // Add your Password Reset service
     PasswordResetService
  ]
})
  1. Next, import PasswordRestService into the necessary component
import { PasswordResetService } from './password-reset.service';

@Component({
    selector: 'app-password-reset',
    templateUrl: './password-reset.component.html',
    styleUrls: ['./password-reset.component.css']
})
export class PasswordResetComponent implements OnInit {
    modalRef: BsModalRef; 
    constructor(
    private modalService: BsModalService,
    private passwordResetService: PasswordResetService
    ) { }

    openAlertModal() {
    this.passwordResetService.alerta("Title", "Message").subscribe((response) => {});
    }
}

Answer №2

1.) To use the PasswordResetService as a standalone class, you need to instantiate it using the new keyword.

2.) If PasswordResetService is set up as an injectable service (using @Injectable decorator), remember to include it in the constructor for proper dependency injection.

Thank you.

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

Troubleshooting issues with sorting and pagination in Angular Material table functionality

I am experiencing an issue with sorting and pagination using an Angular material table. The data is being fetched from a store as an observable and successfully displayed in the table. Even though the column names for sorting match the column definitions, ...

Fixing the "Module not found" error in an Angular library using npm link

I'm currently working on creating an Angular wrapper for a Javascript library, but I've encountered a "Module not found" error. The Javascript library is still in development and has not been published to NPM yet. To work around this issue, I hav ...

What is the method for extracting the types of parameters from a function type while excluding a single parameter?

Suppose I have the following interface that defines a function: export declare interface NavigationGuard { (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): NavigationGuardReturn | Promise<NavigationGuardReturn ...

Encountered a problem with regular expressions in Angular 2 - a Module parse error due to an octal literal in strict mode

Greetings, I have encountered an issue with a regular expression in my environment.ts file. export const environment = { passwordPolicy: "^(?!.*(.)\1\1)(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}.*$" }; Unfortunately, whe ...

A guide on iterating through various JSON arrays and displaying them in AngularJS

I'm in the process of developing a new school management system and I need to showcase the list of departments along with the courses being offered by each department. The course information is saved in JSON format within the database. Here is my JSO ...

Generating a dynamic table using Angular

My goal is to populate a table dynamically using the code below: teams.component.ts import { Component, OnInit } from '@angular/core'; import { first } from 'rxjs/operators'; import { TeamService } from 'src/app/services/team.ser ...

Using the keyof lookup in a Typescript interface is a powerful way to

I'm looking for a solution similar to: interface Operation<T, K extends keyof T> { key: keyof T; operation: 'add' | 'remove'; value: T[K]; } but without the necessity of passing K as a template. Essentially, I want to ...

Error with tsc rootDir due to Docker's installation of yarn in root directory

I am encountering a problem with my node project which serves a simple "hello world" server. Everything runs smoothly when I run it locally, but when I try to run it inside Docker, a /opt/yarn-v1.21.1 folder is created which leads to a rootDir error: erro ...

When attempting to Dockerize an Angular CLI app, nothing seemed to happen

I attempted to dockerize my Angular application and created a Dockerfile as follows: FROM teracy/angular-cli as angular-built WORKDIR /usr/src/app COPY package.json package.json RUN npm install COPY . . RUN ng build FROM nginx:alpine LABEL author="pleijan ...

Dealing with Errors in Angular 8: Best Practices for Handling Response Errors

When an error is thrown from the WEB API, I do not receive any response and the debugger does not hit. However, in case of success, I do get a response and the debugger hits. All I want is to receive an error response and have the debugger hit every time, ...

Using Vue.js 2 on multiple HTML pages with Typescript and ASP.Net Core

My ASP.Net Core MVC project utilizes VueJs2 for more complex tasks, with each view having its own corresponding js file. The directory structure is as follows: ├ Controllers\HomeController.cs (with actions Index & Details) ├ Scripts\Hom ...

How to Initialize Multiple Root Components in Angular 4 Using Bootstrap

We are working on a JQuery application and have a requirement to integrate some Angular 4 modules. To achieve this, we are manually bootstrapping an Angular app. However, we are facing an issue where all the Angular components are loading simultaneously wh ...

Dealing with challenges in integrating ngx-masonry with Angular 14

I am currently working with Angular 14 framework and the ngx-masonry library (https://www.npmjs.com/package/ngx-masonry/v/14.0.1). However, I am facing some issues where it is not functioning correctly. I would appreciate any assistance or guidance on how ...

Using RxJS to merge various HTTP requests into a unified and flattened observable array

Struggling with combining multiple http get requests simultaneously and returning them as a unified, observable array. In my current setup, the method returnNewCars() retrieves Observable<ICar[]> by executing a single http get request. However, in t ...

Combining Typescript and React to create a conditional callback prop type depending on whether an optional prop is

In my react component, I have the option to pass an optional prop called isSingle (boolean) and a required prop called onSelect (callback). If the isSingle prop is used, I want the callback to have a different signature. type CustomProps<T> = { ...

What is the best way to streamline the creation of a "products filter" using Node.js and Angular?

I have decided to build an angular application for an online computer store, and I am using a node/express backend. One of the key features of the application is the products page, where users can view all the products available in our database. Each produ ...

What are some best practices for managing object-level variables in TypeScript and Vue.js?

Uncertain about the optimal approach, I am looking to create a component and leverage some object level variables. Consider the example below: import Vue from "vue" import * as paper from "paper" export default Vue.extend({ template: ` <d ...

Why is the entire rxjs library included in the Angular 9 build bundle?

When I create a new Angular 9.1.1 project and build it without adding any code, the final bundle includes the entire rxjs library. Here is a screenshot from webpack-bundle analyzer The package.json file contains the following dependencies: "dependenci ...

Filter multiple columns in an Angular custom table with a unique filterPredicate

Looking to develop a versatile table that accepts tableColumns and dataSource as @Input(). I want the ability to add custom filtering for each table column. Currently, I've set up the initialization of the table FormGroup and retrieving its value for ...

Converting a file buffer to upload to Google Cloud Storage

I have been facing an issue while attempting to upload a file to Google Cloud using a buffer and the save function. The problem I am encountering is that even though the files are uploaded to Google Cloud, they are not in the correct format. When I try to ...