简体   繁体   English

为什么Angular2 Component属性没有显示在我的页面中?

[英]Why isn't Angular2 Component property showing in my page?

This is my code: 这是我的代码:

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

@Component({
  selector: 'my-app',
  template: `
      <h1>{{title}}</h1>
      <h2>{{secondTitle}}</h2>
      <main-page></main-page>
     `
})

export class AppComponent {
  title = 'Lovely Jubbly';
  secondTitle: 'Bummer...'
}

title 'Lovely Jubbly' appears on my site, but secondTitle 'Bummer...' doesn't show. 我的网站上出现了标题“ Lovely Jubbly”,但是第二标题“ Bummer ...”没有显示。 Only difference is how I initialized those variables. 唯一的不同是我如何初始化这些变量。 I'm TypeScript and Angular2 noob so it might be an obvious mistake. 我是TypeScript和Angular2 noob,所以这可能是一个明显的错误。 Could anyone direct me to some docs that could explain this behaviour? 谁能将我引向一些可以解释这种现象的文档?

TypeScript is interpreting Bummer... as a type (eg, a string that can only be "Bummer..." ). TypeScript将Bummer...解释为一种类型 (例如,只能为"Bummer..."的字符串)。 When it becomes JavaScript, it will still be initialized to null. 当变成JavaScript时,它仍将初始化为null。

If you're wondering, those types can be useful for certain enum-like variables. 如果您想知道,这些类型对于某些类似于枚举的变量可能很有用。 eg: 例如:

var trafficLightState:'RED' | 'GREEN' | 'YELLOW' = 'RED';
trafficLightState = 'Melon'; // ERROR!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM