【问题标题】:Component not showing on page组件未显示在页面上
【发布时间】:2016-12-12 21:18:23
【问题描述】:

我有一个由另一个人开发的Angular2 项目,我需要运行它。当我这样做时,使用npm start 我让服务器运行,但名为my-app 的组件未在页面上呈现。

这是index.html

<html>
  <head>
    <base href='/'>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">    
    <link href="c3.css" rel="stylesheet" type="text/css">

  </head>

  <body>
    <my-app>Loading......</my-app> 
  </body>
</html>

这是app.components.ts

import { Component } from '@angular/core';
import { AuthenticationService } from './authentication.service';
import { Router } from '@angular/router';

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

export class AppComponent {
    title = '';
    isProd:boolean;
    constructor(private authenticationService: AuthenticationService, private  router: Router) {
        this.isProd = false; 
}

templatemy-app,也就是app/app.component.html我放了一个简单的div进行测试:

<div><h1>app components</h1></div>

运行时,我可以看到index.html 中的Loading.....,但看不到app/app.component.html 的内容。

我错过了什么?

【问题讨论】:

  • 检查您的控制台是否有任何错误。
  • 我找不到 c3.css。
  • 什么是AuthenticationService?几天前我遇到了同样的问题,我不得不在@Componet 中添加提供程序,它解决了问题(例如providers: [AuthenticationService]
  • AuthenticationService 只是应用程序内部的一个服务。没什么。我尝试添加提供者:[AuthenticationService, Router],但问题仍然存在。

标签: node.js angular npm


【解决方案1】:

我看不到您是否在 index.html 中的任何位置加载 angular 和您的转译 ts。您需要在 index.html 中加载运行 angular2 应用程序所需的所有依赖项,以及您转译的 ts。

注意:- 在链接加载 css 之后,您需要添加基本标签。

 <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="styles.css">
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">    
        <link href="c3.css" rel="stylesheet" type="text/css">
  <base href='/'>

【讨论】:

  • 加载所有必需的依赖项是什么意思?我正在查看这个示例,embed.plnkr.co/?show=preview,并且 index.html 文件中似乎没有任何内容可以执行加载角度任务。
  • @Eddy 我在这个 plunker 中没有看到任何角度加载? take look at this seed
猜你喜欢
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
相关资源
最近更新 更多