javascript - Angular 2 service injection using TypeScript 1.5 -


i'm trying basic, functional structure set angular 2. have basic api elements framework advances, can advance structure.

currently, i'm @ wit's end how perform simple act of passing services. here example source, taken right comments of recent definitelytyped file:

class greeter {     greet(name: string) {         return 'hello ' + name + '!';     } } @component({     selector: 'greet',     appinjector: [greeter] }) @view({     template: `{{greeter.greet('world')}}!` }) class helloworld {     greeter: greeter;     constructor(greeter: greeter) {         this.greeter = greeter;     } } bootstrap(helloworld); 

as can see, i'm using typescript 1.5 in example.

i have tried inject greeting service in component annotation using hostinjector, injectibles , appinjector. i've tried adding second argument of bootstrap call, in bootstrap(helloworld, [greeter]).

in cases error message when trying run in browser:

error during instantiation of token(componentref)!. original error: cannot resolve parameters helloworld(?). make sure have valid type or annotations.

of course, if remove greeter: greeter argument constructor, error in question goes away , replaced other, expected errors further down chain.

ideas?

edit

i updated title of question specify using typescript 1.5

i had similar problem when using typescript 1.5.0-beta , angular 2.0.0-alpha.28 (not sure if versions strictly relevant) gulp.

you need tell typescript compiler export metadata di injector knows do. did adding emitdecoratormetadata , experimentaldecorators tsconfig.json:

{     "compileroptions": {         "module": "commonjs",         "emitdecoratormetadata": true,         "experimentaldecorators": true,         "sourcemap": true,         "target": "es5"     } } 

after can add appinjector: [greeter] @component declaration you've done or bootstrap declaration:

bootstrap(helloworld, [greeter]); 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -