2017-03-04 29 views
4

Angular-CLIで作成された簡単なアプリケーションをリファクタリングし、app.component.tsコードを別のコンポーネントファイルに移動して、エラー:角2:モジュールが見つかりません:エラー:解決できません

Module not found: Error: Can't resolve on './sb/sb.component.html' ( my SBComponent). Under the

この私が持っているもの:

app.module:

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 

import { AppComponent } from './app.component'; 
import { SBComponent } from './sb/sb.component' 

@NgModule({ 
    declarations: [ 
    AppComponent, SBComponent 
    ], 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule 
    ], 
    providers: [], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 

app.component:

import { Component } from '@angular/core'; 
import { NgModule,ElementRef, ViewChild, AfterViewInit} from '@angular/core' 
import {BrowserModule} from '@angular/platform-browser' 


@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 


} 

新しいコンポーネント:

import { Component, OnInit } from '@angular/core'; 
import { NgModule,ElementRef, ViewChild, AfterViewInit, AfterContentInit} from '@angular/core' 
import {BrowserModule} from '@angular/platform-browser' 


@Component({ 

    selector: 'app-sb', 
    templateUrl: './sb/sb.component.html' 
}) 
export class SBComponent { 



} 

任意の助けいただければ幸いです!

+1

「sb.component.html」はsbフォルダ内に存在しますか?両方が同じフォルダにある場合は、この '。/ sb.component.html'を試してください –

+0

はい、それはsbフォルダ内に存在し、そのフォルダの外にapp.tsが存在します。 – eagleEye

+4

は 'SBComponent'の中で' templateUrl'を 'templateUrl: 'に変更します。/ sb.component.html'' –

答えて

9

コンポーネントが同じディレクトリにある場合は、templateUrlも同じフォルダに指定する必要があります。あなたがそれを行う方法は次のとおりです。

1

この回答は遅くなる可能性がありますが、これは実際にこの問題に直面している他のユーザー向けです。正しい答えを与える必要があります。そのような質問に。

この問題は、外部のhtmlファイルとcssファイルにコンポーネントの相対パスを使用している場合に非常にわかります。絶対パスと相対パスの問題は、コンポーネントにメタデータModuleIdを追加するだけで解決できます。

ModuleIdは何をしていますか?モジュールIDは、このModuleId値はコンポーネントの前に完全修飾成分のパスを評価するために角度反射プロセスおよびmetadata_resolverコンポーネントによって使用さ

[モジュール・ファイルが実際にロードされている]の成分クラスの絶対URLが含まれてい構築されます

非常に使いやすいです。 @Component装飾にもう1つの項目を追加するだけです。以下のようにフルコード。

@Component({ 
    moduleId: module.id, // fully resolved filename; defined at module load time 
    selector: 'app-sb', 
    templateUrl: 'sb.component.html' //Observe this. 
}) 
export class SBComponent { 

} 
+0

を助けた@eagleEye嬉しい:@Component({ モジュールID:module.id、 セレクタ: '私の-デモ'、 templateUrl: './input-text-validation.html' }) –

関連する問題