Angular 2 getting error while adding custom decorator with the line “import 'reflect-metadata'”
See original GitHub issueI’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
Current behavior
Importing reflect-metadata on a Accessor Decorator cause this error:
Unexpected value 'AppComponent' declared by the module 'AppModule'
.
Expected behavior
Reproduction of the problem I’ve downloaded the tour-of-heroes project from github and i’ve added this file:
//Validate.ts
import "reflect-metadata";
export function Validate<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) {
let set = descriptor.set;
descriptor.set = function (value: T) {
}
};
Then i’ve edited the hero-detail.component.ts file like this:
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Hero } from './hero';
import { HeroService } from './hero.service';
import { Validate } from './Validate'; // Added
@Component({
moduleId: module.id,
selector: 'my-hero-detail',
templateUrl: 'hero-detail.component.html',
styleUrls: ['hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {
@Input() hero: Hero;
@Output() close = new EventEmitter();
error: any;
navigated = false; // true if navigated here
private _test: string; // Addedd
constructor(
private heroService: HeroService,
private route: ActivatedRoute) {
}
ngOnInit(): void {
this.route.params.forEach((params: Params) => {
if (params['id'] !== undefined) {
let id = +params['id'];
this.navigated = true;
this.heroService.getHero(id)
.then(hero => this.hero = hero);
} else {
this.navigated = false;
this.hero = new Hero();
}
});
}
@Validate // Addedd
set test(test: string){ // Addedd
this._test = test; // Addedd
} // Addedd
save(): void {
this.heroService
.save(this.hero)
.then(hero => {
this.hero = hero; // saved hero, w/ id if new
this.goBack(hero);
})
.catch(error => this.error = error); // TODO: Display error message
}
goBack(savedHero: Hero = null): void {
this.close.emit(savedHero);
if (this.navigated) { window.history.back(); }
}
}
I’ve also edited the system-config.ts by adding this line:
const map: any = {
'app' : 'app',
'main': 'app/main.js',
'@angular' : 'node_modules/@angular',
'angular2-in-memory-web-api' : 'node_modules/angular2-in-memory-web-api',
'rxjs' : 'node_modules/rxjs',
'reflect-metadata': 'node_modules/reflect-metadata/Reflect.js' // Added
};
What is the motivation / use case for changing the behavior? Use of Accessors Decorators
Please tell us about your environment: Windows 10
- Angular version: 2.0.0
- Browser: all
- Language: Typescript 2.0.2
- Node (for AoT issues):
node --version
= 4.6.0
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Angular 2 (2.0.0 to 2.2.0~) getting error while adding custom ...
I fixed the similar error ( Unexpected value 'XXX' declared by the module 'AppModule' ) by removing the import 'reflect-metadata'; statement and installing ......
Read more >Angular AoT Custom Decorator Error encountered resolving ...
Coding example for the question Angular AoT Custom Decorator Error ... getting error while adding custom decorator with the line "import 'reflect-metadata'" ...
Read more >Documentation - Decorators - TypeScript
TypeScript Decorators overview. ... Decorators are a stage 2 proposal for JavaScript and are available as an experimental ... import "reflect-metadata";.
Read more >TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, ... import "reflect-metadata" import { DataSource } from "typeorm" import ......
Read more >Dependency Injection using Decorators | by Chidume Nnamdi
We won't go into their details here. To go in-depth on the mechanics of Angular-Dependency Injection system, I'd recommend: ... import 'reflect-metadata'
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your question sounds like a support request.
Please use the issue tracker only for bugs and feature requests.
Use gitter and StackOverflow for support request.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.