Importing a module that is not in or around the same folder is quite a nuisance. You have to keep counting the '../'. Like in the example below:
import {AnswersService, AddAnswerModel, Answer} from '../../../../../../../BackendServices/AnswersService';
By modifying my System.config to the example below, I can get around having all of these '../' and the code works perfectly on the browser.
System.config({
packages: {
'app': { defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' }
},
paths: {
'rxjs/*': 'node_modules/rxjs/*',
'BackendServices/*': 'app/BackendServices/*'
}
});
It reduces the import statement to the manageable command below.
import {AnswersService, AddAnswerModel, Answer} from 'BackendServices/AnswersService';
But the problem with this approach is that I lose intellisense in Visual Studio Code. I am not sure if this is a typescript problem, a visual studio code problem, or something else.
Does anyone know how to get this working without losing intellisense?
To import a module from another path, you first need to import the sys module as well as any other Python modules that you would like to use in your program. The sys module is provided by the Python Standard Library and it provides functions and parameters that are system-specific. The path.
We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory. As sys.
The 'module not found' error is a syntax error that appears when the static import statement cannot find the file at the declared path. This common syntax error is caused by letter-casing inconsistencies that are present in your filename(s) between your repository and local machine, or both.
A Python module is a file that has a . py extension, and a Python package is any folder that has modules inside it (or, in Python 2, a folder that contains an __init__.py file).
You can copy a pattern that is implemented by the angular-cli
by exporting every component you want available elsewhere through an index.ts
at the directory level. This way you can expose nested components at a higher level.
Here's an example:
parent
├── child
│ ├── child.component.ts
│ └── index.ts
├── parent.component.ts
└── index.ts
Inside child/index.ts
simply export relevant components.
export { ChildComponent } from './child.component';
Then parent/index.ts
can continue the export chain.
export { ParentComponent } from './parent.component';
export { ChildComponent } from './child';
NOTE: Notice how the above is not referencing the child.component
file, but the child
directory instead!
The child
directory's index.ts
surfaces these components via the exports in index.ts
. This can be done for however deeply nested your components are.
Lastly, if another component outside of parent/
wanted to consume anything inside of parent/
, it could be referenced easily at the topmost level.
import { ParentComponent, ChildComponent } from './parent';
Visual Studio can only resolve relative paths and modules located in node_modules like angular2/*
or rxjs/*
.
It is the TypeScript default moduleResolution ( node ) .. you can change it in tsconfig.json with 'classic' .. but VS Code will no longer recognize node modules.
The problem is discussed in this ticket https://github.com/Microsoft/TypeScript/issues/5039
There are many proposals .. but nothing is implemented yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With