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

Best practices for implementing dual ngFor directives within a single tr element?

Click here to view the page The image attached shows the view I want to iterate through two ngFor loops inside the tr tag. When using a div tag inside the tr, it's looping the button next to the tag instead of where I want it in the file table header ...

Endlessly triggering a function with React Context

I am facing an issue with the profile loading function in my context. It seems to be executing repeatedly instead of only once or when necessary. For instance, I have two modals - one for cases where no profile exists and another for cases where a profil ...

Ways to navigate to a routerlink in Angular 2 without passing any parameters

Struggling with accessing a routerlink in Angular 2 without its parameters. The goal is to use the routerlinks to determine whether or not to display a specific element in the navigation. For normal routerlinks without parameters, I do it like this: *ngIf ...

Guide to personalizing NbLayoutComponent through the creation of a new class extension

Is there a way to make the footer full screen width instead of just the contents section in my app? I am using Nebular for building it. I have tried creating a custom component by extending nbLayoutComponent, but I'm not sure if this is the correct a ...

I am looking to fetch a zip file within the MEAN stack framework

Here is frontend code (Angular):- download(user) { this.clientAPI.get(Endpoints.DOWNLOAD).toPromise() .then(res => { let blob = new Blob([new Uint8Array(res.file)], { type: 'application/zip' }); console.l ...

Utilize Typescript/Javascript to utilize the Gmail API for sending emails via email

I am trying to send emails from my application using my Gmail account with Ionic. I have followed tutorials from SitePoint and Google Developers. Here is how I'm initializing the client: client_id: gapiKeys.client_id, discoveryDocs: ["https://www.goo ...

Navigating through sections in NextJS-14: Utilizing useRef for seamless scrolling

In the past, I had developed an older portfolio website using Vite React + TS and implemented useRef for scrolling to sections from the Navbar. Now, my goal is to transition this portfolio to NextJS 14. I transferred my old components and style folders in ...

Issue with accessing custom method in subclass in Typescript

Recently delving into TypeScript, I decided to subclass Error and add a new method called getCode() in my MyError class. export class MyError extends Error { public code: number; constructor(code: number, message?: string) { super(mes ...

What is the reason behind the lag caused by setTimeout() in my application, while RxJS timer().subscribe(...) does not have the same

I am currently working on a component that "lazy loads" some comments every 100ms. However, I noticed that when I use setTimeout for this task, the performance of my application suffers significantly. Here is a snippet from the component: <div *ngFor ...

Issue with Angular currency code incompatibility on outdated Angular version

I am currently on Angular 7.3.x and unfortunately updating is not an option at the moment. I'm curious if this version limitation could be causing my issue. In essence: {{ value | currency:'USD' }} shows me $ {{ value | currency:'EUR ...

Angular2 and the Use of Environment Variables

I am working on an angular 2/4 app with a node server.js to serve it. I need to access an environment variable for switching between backend endpoints (using localhost for dev and another endpoint for prod). Both the frontend and backend apps are destined ...

Angular 2, using Jasmine and Karma for testing, encountered an error with the HTTP testing API, which returned an error message stating: {"isTr

I've set up an http test case for my Angular2 app using Jasmine-Karma and encountered an API error: Uncaught API Error. Scenario: I'm trying to test an http service for my Angular2 App using Karma-Jasmine, and I've implemented the method be ...

Customizing modal window HTML in ng-bootstrapNeed to override the default modal window

Currently, I am utilizing ng-bootstrap to create my modals. I have been pondering the most effective approach to customize the modal window's HTML. The default appearance of the modal html is somewhat like this: <div role="document" class="modal- ...

Retrieve files by utilizing the find() function in couchdb-nano

As CouchDB doesn't have collections, I decided to add a custom type property to my entities. Now, I want to filter all the entities based on that property, for example, retrieve all users with {type:'user'}. In the CouchDB documentation, I c ...

Using boolean value as default input value in React

When trying to set the default value of a controlled checkbox input from my request, I encountered an error stating "Type 'boolean' is not assignable to type 'string | number | readonly string[] | undefined'". Do you have any suggestion ...

What is the most efficient method to retrieve an API in Angular?

Recently, I dedicated some time to a personal Angular project. While working on it, I decided to experiment with making an API call to PokeAPI in order to retrieve the .svg image of a Pokemon along with its name. After successfully implementing this featur ...

Verify the specific type conditions of a key value in a mapped type

I am attempting to achieve the following: If the actions property exists, and there are callbacks within the actions properties that return a string, then return X or Y. The above code is expressed as: // type MappedType<T> = { // [K in keyof T]: ...

Trigger a (click) or (keyup) event in Angular 4+ only when the value meets the specified pattern

Here is the HTML code snippet. The keyup event will only be triggered if the value in the input field matches the specified pattern. <input (keyup)="checkPatternMatch(ProjectName)" type="text" #ProjectName="ngModel" name="pro-name" class="form-control" ...

Learn how to set up a class using TypeScript decorators

Is there a way to automatically initialize a class when a specific decorator is present above the class? For example: @apiController export class usersControllers extends lib.baseClasses.apiControllerBase().apiController { @lib.decorators.routesRegist ...

Encountering an Uncaught TypeError when attempting to set properties of null with useRef

I've been working on an app that requires access to the user's device camera in order to display live video on the screen. I've managed to achieve this by utilizing a video HTML Object and setting the media stream as its srcObject. Everythin ...