问题描述:

I'm fairly new to Aurelia so please be gentle with me if this is an obvious question or even something one should not consider doing. From reading the documentation and various recources on the web this does not seem to be addressed anywhere.

As my project increased in size I restructured folders and files. While refactoring it felt a bit cumbersome, as to check te correct depth of the path, and also when I moved view models I needed to change the import path as well.

Currently I need to import certain files in my view models as:

import {log} from './../../services/log';

What I would find more comfortable would be to have a relative path starting from the root of the project like:

import {log} from 'services/log';

Is there anything I'm missing or simply not understanding? I know that with the ./ the relative path from the current file is specified.

Update:

I tried the same with the Aurelia Contact Manager Tutorial where all files in the src folder are on the same level. If I move the 'wep-api.ts' file into a 'src/services' folder and want to import that file from a viewmodel inside 'src/components/users' I need to use the import as

import {WebAPI} from './../../services/web-api';

It does not seem to work with only 'services/web-api' and the error is

[ts] cannot find module 'services/web-api'

The aurelia.json file includes

"paths": {

"root": "src", ...

网友答案:

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 {
相关阅读:
Top