Error:Decorators are not valid here. 使用Angular中的装饰器

Decorators are not valid here,项目中出现这个提示信息,说明装饰器未知错误、或者在不支持的元素上使用了装饰器。

如下图所示,我在@NgModule装饰器后面加了一个导出方法,加完之后控制台提示了如下错误:Error TS1206:Decorators are not valid here.。@NgModule装饰器应该直接放在类定义之前,而代码中装饰器放在了方法前面,所以装饰器在这里无效:

修改对应位置的代码,@NgModule装饰器放在SharedModule前面,把getEcharts方法放在装饰器的前面或者类定义的后面,就好了:

在Angular中,装饰器是用来增强类、方法、属性和参数的强大工具。它们允许你添加元数据,改变行为,并使代码更具可读性和可维护性,在使用装饰器时,要知道使用了哪个装饰器,装饰器用在了哪个元素上。

常见的装饰器有下面几种,它们分别对应着一种使用情景

  • @Component:定义一个组件
ts 复制代码
import { Component } from '@angular/core';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})
export class MyComponent {
  // ...
}
  • @Directive:定义一个自定义指令
ts 复制代码
import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  constructor(private el: ElementRef) {
    el.nativeElement.style.backgroundColor = 'yellow';
  }
}
  • @NgModule:定义一个Angular模块
ts 复制代码
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  • @Injectable:定义一个Angular服务
ts 复制代码
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class MyService {
  // ...
}
  • @Input:定义一个组件的输入属性
ts 复制代码
import { Component, Input } from '@angular/core';

@Component({
  // ...
})
export class MyComponent {
  @Input() name: string;
}
  • @Output:定义一个组件的输出属性
ts 复制代码
import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  // ...
})
export class MyComponent {
  @Output() onButtonClicked = new EventEmitter<any>();

  onClick() {
    this.onButtonClicked.emit('Button clicked!');
  }
}
  • @HostListenter:鉴定宿主元素的事件
ts 复制代码
import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[appClick]'
})
export class ClickDirective {
  @HostListener('click') onClick() {
    console.log('Click event!');
  }
}
  • @HostBinding:绑定宿主元素的属性
ts 复制代码
import { Directive, HostBinding } from '@angular/core';

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  @HostBinding('style.backgroundColor') backgroundColor = 'yellow';
}
  • @Pipe:定义一个Angular管道
ts 复制代码
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'capitalize'
})
export class CapitalizePipe implements PipeTransform {
  transform(value: string): string {
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
}
  • @ViewChild:获取模板中子组件的引用
ts 复制代码
import { Component, ViewChild } from '@angular/core';
import { MyComponent } from './my-component.component';

@Component({
  // ...
})
export class ParentComponent {
  @ViewChild(MyComponent) myComponent: MyComponent;

  // ...
}
  • @ContentChild:获取模板中内容投影的引用
ts 复制代码
import { Component, ContentChild } from '@angular/core';
import { MyComponent } from './my-component.component';

@Component({
  // ...
})
export class ParentComponent {
  @ContentChild(MyComponent) myComponent: MyComponent;

  // ...
}
  • @Inject:在依赖注入时指定要注入的依赖项
ts 复制代码
import { Injectable, Inject } from '@angular/core';
import { MY_TOKEN } from './my-token';

@Injectable({
  providedIn: 'root'
})
export class MyService {
  constructor(@Inject(MY_TOKEN) private myValue: string) {
    // ...
  }
}
  • @Optional:在依赖注入时指定依赖项是可选的
ts 复制代码
import { Injectable, Inject, Optional } from '@angular/core';
import { MY_TOKEN } from './my-token';

@Injectable({
  providedIn: 'root'
})
export class MyService {
  constructor(@Optional() @Inject(MY_TOKEN) private myValue: string) {
    // ...
  }
}
  • @SkipSelf:在依赖注入时跳过自身的提供者
ts 复制代码
import { Injectable, Inject, SkipSelf } from '@angular/core';
import { MY_TOKEN } from './my-token';

@Injectable({
  providedIn: 'root'
})
export class MyService {
  constructor(@SkipSelf() @Inject(MY_TOKEN) private myValue: string) {
    // ...
  }
}
  • @Self:在依赖注入时只查找自身提供者
ts 复制代码
import { Injectable, Inject, Self } from '@angular/core';
import { MY_TOKEN } from './my-token';

@Injectable({
  providedIn: 'root'
})
export class MyService {
  constructor(@Self() @Inject(MY_TOKEN) private myValue: string) {
    // ...
  }
}
  • @forwardRef:解决循环依赖问题
ts 复制代码
import { forwardRef, Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class AService {
  constructor(private bService: BService) {
    // ...
  }
}

@Injectable({
  providedIn: 'root'
})
export class BService {
  constructor(private aService: AService) {
    // ...
  }
}
ts 复制代码
import { forwardRef, Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class AService {
  constructor(private bService: BService) {
    // ...
  }
}

@Injectable({
  providedIn: 'root'
})
export class BService {
  constructor(@Inject(forwardRef(() => AService)) private aService: AService) {
    // ...
  }
}
相关推荐
「、皓子~25 分钟前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
就改了27 分钟前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_29 分钟前
Ajax 入门
前端·javascript·ajax
京东零售技术44 分钟前
京东小程序JS API仓颉改造实践
前端
奋飛1 小时前
TypeScript系列:第六篇 - 编写高质量的TS类型
javascript·typescript·ts·declare·.d.ts
老A技术联盟1 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
风铃喵游1 小时前
构建引擎: 打造小程序编译器
前端·小程序·架构
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟1 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor1 小时前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js