Why my file app.component.html is not working?

廉价感情. 提交于 2020-01-15 11:56:07

问题


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:

enter image description here

enter image description here

I'm using angular 6 with an ASP.NET CORE 1.1 application.


回答1:


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>




回答2:


You don't need <!DOCTYPE html> <html> <head> <body> tags outside index.html since the html in component.html is inserted where <my-app></my-app> tag is.

With the component.html content, the result page will be:

<!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>
      <!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <title></title>
      </head>
      <body>
         abc

      </body>
      </html>

  </body>
</html>

hence invalid, because you will have two of theses tags: <!DOCTYPE html> <html> <head> <body>



来源:https://stackoverflow.com/questions/52445476/why-my-file-app-component-html-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!