为什么我的文件 app.component.html 不起作用? [英] Why my file app.component.html is not working?
问题描述
我的基本 Angular 应用没有显示 app.component.html 内容,我总是有 index html 的内容.
My basic angular app is not displaying the app.component.html content, always I have the content of index html.
索引 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>
应用组件测试:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: 'wwwroot/app.component.html'
})
export class AppComponent { title = 'My First Angular App!'; }
应用模块 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 { }
应用组件 html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
abc
</body>
</html>
结果:
我在 ASP.NET CORE 1.1 应用程序中使用 angular 6.
I'm using angular 6 with an ASP.NET CORE 1.1 application.
推荐答案
app.component.html 不是网页的根,而是 Angular 应用的根.index.html 是您的 <html>
标签应该存在的唯一地方.index.html 包含 <my-app></my-app>
标签,它是你的 Angular 应用程序的根.
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.
如果你把 放在你的 angular.component.html 中,当你的 angular 应用程序在运行 'ng start' 编译后的网页后放入 index.html看起来像这样:
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.
为了让您的示例工作,从 app.component.html 中取出以下标签:
<代码><头><meta charset="utf-8"/><标题>和
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>
这篇关于为什么我的文件 app.component.html 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!