“ import { Component } from’@angle/core’;”语句中的“@”符号是什么意思?

我正在读角度2的 “5分钟快速启动”,有这么一行:

import { Component } from '@angular/core';"

我不明白,@符号在这个导入中代表什么? TypeScript 文档也没有提到这一点。

这是什么意思?

32261 次浏览

this is just a naming convention that Angular uses. Since the release they renamed it to @angular/core in stead of angular2/core.

It references the core components of the framework.

(found in post - angularjs 2 with angular-material @angular/core not found)

@scope_name/package_name

This is NPM feature, scoped name, anything between @ and slash / will be your scope name.

npm scope document

Also of relevance is that you can use the @ symbol scoping for non-npm packages as well. You can use this in your project as a short way of referring to different directories.

i.e.

import { MyService } from '@services/my.service';
import { HelloWorldComponent } from '@components/hello-world.component';

instead of

import { MyService } from '../../../../my.service';
import { HelloWorldComponent } from '../shared/deeply/nested/hello-world/hello-world.component';

To do this, you simply configure your tsconfig.json file (at root of project) like this:

{
"compileOnSave": false,
"compilerOptions": {


// omitted...


"baseUrl": "src",
"paths": {
"@services/*": ["app/path/to/services/*"],
"@components/*": ["app/somewhere/deeply/nested/*"],
"@environments/*": ["environments/*"]
}
}
}

See the full details at Angular Firebase