【问题标题】:Angular 2 Typescript Typings Path IssueAngular 2 Typescript Typings 路径问题
【发布时间】:2016-12-07 05:33:19
【问题描述】:

我正在尝试使用 Angular 2 和 Typescript 学习和创建 Gulp 构建过程。我遵循了快速入门,一切正常。现在我正在尝试分支并尝试以不同的方式做事。我想要做的是使用不同的文件夹结构,但我的 app.component.ts 文件查找 import { Component } from 'node_modules/@angular/core'; 时遇到问题。我已将文件更改为 import { Component } from '../../../manager/node_modules/@angular/core'; 并且它可以工作,但我看到其他人有不同的文件夹结构并且他们不使用长目录路径。

目前我的文件夹结构如下:

build
  - css
  - js
  // all compiled files end up here
manager
  - node_modules
  - typings
  gulpfile.ts
  typings.json
  package.json
  tsconfig.json
source
  app.component.ts
  main.ts

app.component.ts

/// <reference path="../manager/typings/index.d.ts" />

import { Component } from '../manager/node_modules/@angular/core';
@Component({
  selector: 'my-app',
  template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }

我的问题是:

  • 我可以引用@angular/core而不用写出整个路径吗?

  • 有没有办法可以省略&lt;reference path=""&gt;

angular2-seed 项目可以做到,但我不知道他们是如何做到的。我确定他们有一个文件可以为他们做文件参考。

【问题讨论】:

    标签: javascript angularjs typescript angular


    【解决方案1】:

    尝试使用以下文件夹结构:

    build/
      css/
      js/
    source/
      app.component.ts
      main.ts
    node_modules/
    typings/
    gulpfile.ts
    typings.json
    package.json
    tsconfig.json
    

    说明:您的tsconfig.json 必须在根目录中,因此TypeScript 编译器将正确解析@angular/core 模块。将node_modules 文件夹和typings.json 文件也保留在根目录中也是一个好主意。

    要了解 TypeScript 如何解析模块,请阅读Module Resolution 文档。

    【讨论】:

    • 我很高兴@Mike :)
    猜你喜欢
    • 2016-10-04
    • 1970-01-01
    • 2016-09-23
    • 2021-03-20
    • 1970-01-01
    • 2020-08-08
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多