Is it possible to specify paths from project root in aurelia view models? Is it possible to specify paths from project root in aurelia view models? typescript typescript

Is it possible to specify paths from project root in aurelia view models?


As it appears the typescript compiler needs to be configured to access the desired path.

tsconfig.json

{   ...  "compilerOptions": {    ...    "baseUrl": ".",    "paths": {      "*":["src/*"]    }  }  ...}

With this in place, imports now work as e.g. 'services/web-api' (located in 'src') from any file and folder structure below 'src'.

Kudos go to stoffeastrom from the Aurelia gitter channel who actually pointed out the right direction.


This should work just fine. The documentation on Aurelia's Doc HUB states:

"The path you supply in the from attribute can take one of two forms: It can be relative to the root of your application or relative to the path of the view you are currently in. A path that does not have a ./ or ../ to start the path will be relative to the root of your application, while a path with ./ or ../ will be relative to your view's path. Note that you can use multiple ..'s to traverse up a directory tree as needed."

This works correctly in practice as well (definitely with aurelia-cli, which is what I use).

Therefore, the statement as you wrote it:

import {log} from 'services/log';

will effectively import a viewmodel (log.ts) and view (log.html, unless the @noView decorator is specified) from the /src/services folder.

Class Only (No View)

If you don't have a view, it will throw an error unless you explicitly tell Aurelia not to look for one. To work around this, import noView and add the decorator before your class declaration, like this:

import {noView} from 'aurelia-framework';@noView export class MyClass {