Import component works when in app folder but not

2019-09-04 03:31发布

问题:

I've got an angular 2 component that when I put in app-name/src/app/component-folder/component.ts and import like import {Component} from './component-folder/component' works just fine.

But when the component is installed to the app-name/node_modules folder at app-name/node_modules/component-folder/component and I do

import {Component} from 'component-folder/component'

or import {Component} from '../../node_modules/component-folder/component'

It gives me this error: Unexpected value 'Component' declared by the module 'AppModule'

Why is that and how do I fix it?

回答1:

Because module resolution by tsc is different when it looks for something from node_modules. https://www.typescriptlang.org/docs/handbook/module-resolution.html