仅来自root / app文件夹的Angular 2加载组件

时间:2016-06-26 17:59:24

标签: javascript angular

我用angular-cli创建了一个angular2项目(版本1.0.0-beta.8,角度版本是2.0.0-rc.3) 运行ng newng init会创建此目录结构:

  create .editorconfig
  create README.md
  create src\app\app.component.css
  create src\app\app.component.html
  create src\app\app.component.spec.ts
  create src\app\app.component.ts
  create src\app\environment.ts
  create src\app\index.ts
  create src\app\shared\index.ts
  create src\favicon.ico
  create src\index.html
  create src\main.ts
  create src\system-config.ts
  create src\tsconfig.json
  create src\typings.d.ts
  create angular-cli-build.js
  create angular-cli.json
  create config\environment.dev.ts
  create config\environment.js
  create config\environment.prod.ts
  create config\karma-test-shim.js
  create config\karma.conf.js
  create config\protractor.conf.js
  create e2e\app.e2e-spec.ts
  create e2e\app.po.ts
  create e2e\tsconfig.json
  create e2e\typings.d.ts
  create .gitignore
  create package.json
  create public\.npmignore
  create tslint.json
  create typings.json

当我生成一个组件(ng g组件my-component)时,它会在src \ app文件夹中添加一个文件夹my-component,其中包含组件文件(ts,css,html等...)

当我在app(主要的)组件中导入my-component组件并将其放在html中时:

import { Component } from '@angular/core';
import { MyComponentComponent } from './my-component';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  directives: [MyComponentComponent]
})
export class AppComponent {
  title = 'app works!';
}

app.component.html:

<h1>
  {{title}}
</h1>
<app-my-component></app-my-component>
一切正常。

如果我创建一个文件夹(在这种情况下名为“project”)并在那里移动my-component文件夹(src\app\project\my-component)并从那里导入组件:

import { Component } from '@angular/core';
import { MyComponentComponent } from './project/my-component'; //chnaged "./my-component" to "./project/my-component"

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  directives: [MyComponentComponent]
})
export class AppComponent {
  title = 'app works!';
}

应用程序已编译,但在浏览器中我得到:

zone.js:101 GET http://localhost:4200/app/project/my-component.js 404 (Not Found)

这是一个错误还是我错过了什么?

编辑:

无论文件夹如何,都可以正确编译所有组件。

奇怪的是,应用程序想要在生成的文件为my-component.js时加载my-component.component.js(这与其他名称的作用相同。名称中的“component”不是问题)

当组件文件夹位于app文件夹中时,应用程序正确加载my-component.component.js

对我来说看起来像个错误。

2 个答案:

答案 0 :(得分:1)

检查新文件夹(项目)中是否有组件打字稿编译文件(* .js和* js.map)

另请参阅组件相对URL到组件模板和样式文件的代码:

  

Angular 2 CLI使用这些技术并默认使用   组件相对路径方法   这里描述。 CLI用户可以跳过本章或继续阅读   了解它是如何工作的。

https://angular.io/docs/ts/latest/cookbook/component-relative-paths.html

<强> UPDATE1

1)创建一个新文件夹&#34; project&#34;在src / app中

2)在终端中运行:

ng generate component project/my-component2

(见:https://github.com/angular/angular-cli#generating-components-directives-pipes-and-services

这将生成相对于此新文件夹(项目)的组件和所有依赖项

3)更改你的app.component.ts和html以查看组件。

以下是2个组件的示例代码(src / app中为1,src / app / project /中为1):

我-component.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app-my-component',
  templateUrl: 'my-component.component.html',
  styleUrls: ['my-component.component.css']
})
export class MyComponentComponent implements OnInit {

  constructor() {}

  ngOnInit() {
  }

}

我-component2.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app-my-component2',
  templateUrl: 'my-component2.component.html',
  styleUrls: ['my-component2.component.css']
})
export class MyComponent2Component implements OnInit {

  constructor() {}

  ngOnInit() {
  }

}

<强> app.component.ts

import { Component } from '@angular/core';
import { MyComponentComponent } from './my-component/my-component.component';
import { MyComponent2Component } from './project/my-component2/my-component2.component';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  directives: [
    MyComponentComponent,
    MyComponent2Component
  ]
})
export class AppComponent {
  title = 'app works!';
}

<强> app.component.html

<h1>
  {{title}}
</h1>
<app-my-component></app-my-component>
<app-my-component2></app-my-component2>

对我来说这很好用

enter image description here enter image description here

答案 1 :(得分:0)

Probaly你的错误是在编译时(从.ts到.js)

我的意思是当你创建组件时它可以正常工作,但当你将组件切换到另一个名为project的文件夹(无论如何)时,angular会尝试从app/project/my-component.js找到文件,但你现有的文件位置是{ {1}}(请参阅您的dist文件夹中)。所以,只需停止项目并尝试使用重新运行项目,这样所有文件都将生成具有确切位置的项目并且您的项目正常运行