angular - Why my file app.component.html is not working?
问题描述
My basic angular app is not displaying the app.component.html content, always I have the content of index html.
Index HTML:
<!DOCTYPE html>
<html>
<head>
<title>Angular 6</title>
<base href="/">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('main.js').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<my-app></my-app>
</body>
</html>
App Component ts:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: 'wwwroot/app.component.html'
})
export class AppComponent { title = 'My First Angular App!'; }
App Module ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
app component html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
abc
</body>
</html>
Result:
I'm using angular 6 with an ASP.NET CORE 1.1 application.
解决方案
app.component.html isn't the root of your web page, it's the root of your angular app. index.html is the only place where your <html>
tag should be. index.html contains the <my-app></my-app>
tag that is the root of your angular app.
If you put <html></html>
in your angular.component.html, when your angular app is put into index.html after running 'ng start' your compiled web page will look something like this:
<html> <!-- from index.html -->
...
<my-app>
<html> <!-- from app.component.html -->
...
</html>
</my-app>
</html>
and your webpage can't have two <html>
root elements in it.
To get your example working take the following tags out of app.component.html:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title> and <body>
推荐阅读
- jquery - 初始化映射();或重绘数据表的任何自定义函数
- amazon-dynamodb - DynamoDB 中的 `BatchWriteItem` API 的“16 MB 数据限制”实际上是什么?
- python - 是否有任何解决方案可以在 selenium Python 和使用 chromedriver 的无头浏览器中捕获具有不同 URL 的屏幕截图?
- javascript - event.stopPropagation() 在 react 17.0.1 中没有按预期工作?
- amazon-web-services - 当 IAM 用户在多个组中时,如何协调权限?
- android - 无法在 Flutter 中集成 Google 移动广告
- asp.net-core - PageModel(razor pages) 是如何处理的?
- python - 如何使用对话处理程序在 Python 中获取用户输入(python-telegram-bot)
- gif - 保存 GIF 并保留路径 - 有可能吗?
- javascript - 即使关闭,音频也会自动播放