财产名称'类型' MyComponent'中不存在

时间:2017-02-28 09:24:31

标签: angular typescript

我是angular2的新手,不知道关于1的事情,请告诉我为什么它会向我显示此错误以及如何解决此问题。

import { Component } from 'angular2/core';

@Component
({

  selector: 'my-component',

  template: `<div>Hello my name is {{name}}. <button (click)="sayMyName()">Say my name</button></div>`
})


export class MyComponent

 {
  constructor() {
   this.name = 'Max'
  }

  sayMyName() 
{

    console.log('My name is',this.name)
  }
}

2 个答案:

答案 0 :(得分:1)

您需要在引用属性之前声明属性

export class MyComponent {
  name:string; // <<<===
  constructor() {
   this.name = 'Max'
  }

答案 1 :(得分:0)

您需要先声明您的财产

export class MyComponent {
    name: string;

    constructor() {
        this.name = 'Max'
    }

    sayMyName() {
        console.log('My name is',this.name)
    }
}