Error in Angular: The use of decorators in this context is not allowed.ts(1206)

In my current project using Angular 17 and PrimeNG 17, I am implementing a theme switching feature. I have been following a tutorial from the Primeng documentation at this link: https://www.youtube.com/watch?v=5VOuUdDXRsE&embeds_referring_euri=https%3A%2F%2Fprimeng.org%2F&source_ve_path=Mjg2NjY&feature=emb_logo. Below is the code snippet from my theme.service.ts:

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { SystemService } from './system.service';

@Injectable({
  providedIn: 'root',
})
export class ThemeService {
  constructor(
    @Inject(DOCUMENT) private document: Document,
    private systemService: SystemService
  ) {}

  switchTheme(theme: string): void {
    let themeLink = this.document.getElementById(
      'app-theme'
    ) as HTMLLinkElement;

    if (themeLink) {
      themeLink.href = 'assets/themes/' + theme + '/theme.css';
      this.systemService.setLocalStorage('theme', theme);
    }
  }
}

Although the app is functioning properly and the theme switching functionality works as intended, I encounter an error message stating

Decorators are not valid here.ts(1206)
at the @Inject decorator.

I am seeking a solution to address this error or possibly disable the warning altogether. Thank you for any guidance provided.

Answer №1

I attempted to reproduce the issue on stackblitz, but everything seems to be functioning correctly for me. Have you considered making the following change?

import { inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { SystemService } from './system.service';

@Injectable({
  providedIn: 'root',
})
export class ThemeService {
  document: Document = inject(DOCUMENT);

  constructor(
    private systemService: SystemService
  ) {}
  ...

Here is a link to the Stackblitz demo where the theme switcher is working perfectly:

Stackblitz Demo

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

Maximizing the efficiency of enums in a React TypeScript application

In my React application, I have a boolean called 'isValid' set like this: const isValid = response.headers.get('Content-Type')?.includes('application/json'); To enhance it, I would like to introduce some enums: export enum Re ...

merging pictures using angular 4

Is there a way in Angular to merge two images together, like combining images 1 and 2 to create image 3 as shown in this demonstration? View the demo image ...

"Exploring the process of extracting data from a JSON URL using NextJS, TypeScript, and presenting the

After tirelessly searching for a solution, I found myself unable to reach a concrete conclusion. I was able to import { useRouter } from next/router and successfully parse a local JSON file, but my goal is to parse a JSON object from a URL. One example of ...

Developing a Typescript "Map" type using numerical enumerations

In my Typescript project, I came across the need to create record types with numeric enums: enum AxisLabel { X = 0, Y = 1 } export const labelLookup: Record<AxisLabel, string> = { [AxisLabel.X]: "X axis", [AxisLabel.Y]: "Y Axis" }; However, I w ...

Integrating a packaging NPM functionality into Angular for streamlined development

After completing a software engineering assignment, I am struggling with the final requirement. I need to implement an NPM packaging command called "npm build" to compile and publish the front end developed in Angular to the backend project. Initially, I t ...

Is there a way to automatically populate the result input field with the dynamic calculation results from a dynamic calculator in Angular6?

My current challenge involves creating dynamic calculators with customizable fields. For example, I can generate a "Percentage Calculator" with specific input fields or a "Compound Interest" Calculator with different input requirements and formulas. Succes ...

Guide on changing the color of the selected item in mat-nav-list within angular 6

Recently diving into Angular 6 and facing an issue with my mat-toolbar integrated with mat-sidenav. Everything seems to be functioning fine, but I'm looking to customize the color for the active item in the side nav menu. Currently, all items have a ...

Leveraging Angular OpaqueToken for Injecting Config Object Does Not Display Type Errors

When using an OpaqueToken to inject a config object into the application in Angular, I followed the documentation and was able to successfully DI the config into a component and retrieve values. However, I encountered an issue when trying to enforce type c ...

ABP's Angular DateTimePicker Component for Effortless Date and Time

I am experiencing an issue with a DateTime field that is supposed to display only the time: HTML: <div class='input-group date'> <input class="form-control" type="datetime" #RequiredByDate name="RequiredByDate" [value]="formatDate(h ...

Angular 5: There was an issue with the property not defined for lowercase conversion

I keep encountering this error whenever I attempt to filter a column of a table. The data is retrieved from a FireStore cloud collection and the 'auteur' field is defined in each document. Here is how my component looks: import { Component, OnI ...

Updating the value of an input field in Angular 2

When I enter 123 in my input field and submit, I want to see 456. Is there a way to change the value of an input programmatically? Here is my HTML code using Ionic2: <ion-textarea [ngFormControl]="message"></ion-textarea> This is the JavaSc ...

Disable the ability to close the dialog box by clicking

this is my dialog <div *ngIf="visible" class="overlay" (click)="close()"> <div role="dialog" class="overlay-content"> <div class="modal-dialog" (click)="$event.stopPropagation()"> <!-- Modal content--> ...

Extracting PNG file from response (bypassing standard JSON extraction)

Despite my efforts to find a solution, I am still unable to resolve this specific issue: I have implemented an Angular request (localhost:4200) to an API on Spring (localhost:8080). The HttpService successfully handles the requests, except when it comes to ...

Implement ExpressTS on vercel platform

I have recently developed an Express TypeScript project on GitHub and now I am attempting to deploy it to Vercel. The folder structure of the project is as follows, with 'dist' containing app.js and 'src' containing app.ts. dist dto mi ...

Expanding unfamiliar categories

Currently, I am working with Gutenberg blocks in a headless manner. Each Gutenberg block is defined by the following structure: type Block = { name: string; className?: string; key?: string | number; clientId: string; innerBlocks: Block ...

The NodeJS backend is receiving an empty request body from the Ionic frontend application

In my Ionic app (Angular2) with a Node backend, I have implemented a feature called Level, where each Level has its own title. Users can view a list of levels and add new ones through a modal. However, I encountered an issue where the titles are not being ...

The type 'typeof globalThis' does not have an index signature, therefore the element is implicitly of type 'any'. Error code: ts(7017) in TypeScript

I'm encountering an issue with my input handleChange function. Specifically, I am receiving the following error message: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017) when att ...

Retrieving Vue component properties as a data type

I'm facing a dilemma with my Vue components. I want to extract the props from one component and use them as a type instead of a value in another component. Specifically, I have a component where I need to take in an array of props from a different com ...

Is Angular's forkJoin being phased out?

Upon opening my Angular project today, I came across a warning message that said: The utilization of forkJoin is marked as deprecated: resultSelector is deprecated, it is recommended to use pipe to map instead (deprecation) After searching on Google, I f ...

Development in Angular 2 with a team of developers utilizing TFVC for version control and managing node_modules

With over 20,000 files in the node_modules directory, it may not be practical to include them in source control. This results in developers having to run 'npm install' every time they perform a 'get latest' in order to download any mis ...