visual studio - Angular2 & typescript get error with import -
i'm starting implement simple component based on angular 2 , issue tsconfig.json
, import
here structure
root | node_modules | | | @angular | | | core | platform-broswer-dynamic script | components | myfirstcomponent.ts myfirstcomponentservice.ts
here code
import { bootstrap } '@angular/platform-browser-dynamic'; // line ok import { component } '@angular/core'; // line ok import { firstservice } 'root/script/components/myfirstcomponentservice'; // line error @component({ selector: 'firstcomponent', template: '<div>my first component</div>', }) export class myfirstcomponent { constructor(public abc : firstservice) { console.log(abc.dosomething()); } } bootstrap(myfirstcomponent, [firstservice]);
but error at
import { abcservice } 'root/script/components/myfirstcomponent';
because reason don't want use import { abcservice } ./myfirstcomponent';
what config should use in tsconfig.json
make 3 import
work ? i've tried rootdir
not help
i'm using vs2015 , typescript 1.8.32
thanks much!
you not need specify full path, service , component both in same file location need use ./
so:
import { firstservice } './myfirstcomponentservice';
edit: going comment, think you're asking this. have sub folder inside root
, , sub folder inside components
, have now:
root | node_modules | | | @angular | | | core | platform-broswer-dynamic script | components | | | myfirstcomponent.ts | myfirstcomponentservice.ts | | | navbar | | | navbar.component.ts | navbar.component.html | shared | authservice.component.ts
if wanted access the navbar.component
inside same file, use:
import { firstservice } './navbar/navbar.component';
you would need specify current folder, ./
, go navbar folder, component there.
now if wanted access authservice.component
, following:
import { firstservice } '../shared/authservice.component';
the reason shared
folder located 1 folder higher current folder you're in, that's why use ../
, takes 1 folder "higher".
does explain better? added random "common" components. maybe consider changing folder structure, , naming folder/components lower case only.
Comments
Post a Comment