Не удалось загрузить server.component.html

#angular7

#angular7

Вопрос:

Это основной файл.ts.

 import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
 .catch(err => console.log(err));
  

Это файл server.component.ts.

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

@Component({
  selector: 'app-server',
  templateUrl: './server.component.html'
})
export class ServerComponent {

}
  

Показывает две ошибки:

  1. ПОЛУЧИТЬ http://localhost:4200/server.component.html 404 (не найден)
  2. Не удалось загрузить server.component.html

Ответ №1:

Это не тот способ отображения компонентов в angular7.

Я думаю, вам не хватает основ angular

Ознакомьтесь с этим начальным руководством, чтобы начать изучать angular. https://angular.io/tutorial

Ответ №2:

В файле server.component.ts просто замените абсолютный путь, указанный в разделе templateUrl, на относительный путь, и он будет работать.

Комментарии:

1. Пробовал использовать как абсолютный, так и относительный путь, но не сработало… (Angular 8)

Ответ №3:

В качестве быстрого решения этой проблемы вы можете просто добавить висячую запятую [две косые черты ( // ) после templateUrl] после templateUrl, как показано ниже.

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

@Component({
  selector: 'app-server',
  templateUrl: './server.component.html' //
})
export class ServerComponent {

}
  

У меня это сработало.

Ответ №4:

Недавно я столкнулся с такой же проблемой, в моем случае я ищу все компоненты, которые ссылаются на ошибку, и нахожу некоторые ошибки при импорте angular:

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

Исправить:

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