Angular 2: Module not found: Error: Can't reso

2019-04-24 12:38发布

I have a simple app created with Angular-CLI, I wanted to refactor and move the app.component.ts code in separate component file and started to get a nasty error:

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

This what i have:

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  {


}

new component:

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  {



}

Any help would be greatly appreciated!

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-24 12:54

This answer may be late for this question, but this is for other users who are actually facing this issue. we should need to give correct answer to such a question.

This problem is very well known, when we are using component relative paths for external html and css files. The problem of absolute path and relative path can be resolved just by adding metadata ModuleId in component.

Now what does ModuleId do? ModuleId contains the absolute URL of the component class [when the module file is actually loaded]

This ModuleId value is used by the Angular reflection processes and the metadata_resolver component to evaluate the fully-qualified component path before the component is constructed

It's very easy to use. Just add one more entry in @Component decorative. Full code example is below.

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

}
查看更多
可以哭但决不认输i
3楼-- · 2019-04-24 12:56

Try to give relative path instead of absolute one .

It will fix the Solution

like : templateUrl = '../employee/employee.html' in your component.ts

查看更多
神经病院院长
4楼-- · 2019-04-24 13:06

If your component is inside the same directory, then you need to point your templateUrl to the same folder as well. Here is how you can do it:

@Component({
    selector: 'app-sb',
    templateUrl: './sb.component.html'
})
查看更多
登录 后发表回答