Angular 2: module not found: error: cannot resolve

I have a simple application created using Angular -CLI, I wanted to reorganize and move the app.component.ts code into a separate component file and started to get an unpleasant error:

Module not found: Error: Cannot resolve "./sb/sb.component.html" (my SBComponent). Under

This is 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!

+11
source share
4 answers

If your component is inside the same directory, you need to point your templateUrlto the same folder. Here's how you can do it:

@Component({
    selector: 'app-sb',
    templateUrl: './sb.component.html'
})
+21
source

.

: templateUrl = '../employee/employee.html' component.ts

+2

, ,

, :

@Component({
    selector : 'app-header',
    templateUrl : './header.component.html',
    styleUrls : ['']
})

here by changing:

styleUrls : ['']

on this:

styles : ['']

helped me remove the not found module, thought someone might get this error due to this stupid error, so shared my solution

0
source

Source: https://habr.com/ru/post/1671434/


All Articles