Javascript 初学者Angular4组件故障排除

Javascript 初学者Angular4组件故障排除,javascript,angular,oop,typescript,Javascript,Angular,Oop,Typescript,****更新代码****** 大家好,angular 4的新手,我正在尝试制作一个简单的组件,显示从中获取数据的月份和日期 这是我的data.model.ts文件 export class Dater { date = new Date(); month: number = this.date.getMonth(); day: number = this.date.getDate(); constructor() { } } import { C

****更新代码******
大家好,angular 4的新手,我正在尝试制作一个简单的组件,显示从中获取数据的月份和日期

这是我的
data.model.ts
文件

export class Dater {

    date = new Date();
    month: number = this.date.getMonth(); 
    day: number = this.date.getDate();

    constructor() {
    }

}
import { Component, OnInit } from '@angular/core';
import { Dater } from './dater.model'; // here i reference my date class
@Component({
  selector: 'app-date',
  templateUrl: './date.component.html',
  styleUrls: ['./date.component.css']
})
export class DateComponent implements OnInit {

  date: Dater; // assign property to type Date class
  constructor() { 
  }
  ngOnInit() {
  }

}

我得到了以下错误
找不到名称“date”您是指实例成员“this.date”吗?

然后是我的
date.component.ts
文件

export class Dater {

    date = new Date();
    month: number = this.date.getMonth(); 
    day: number = this.date.getDate();

    constructor() {
    }

}
import { Component, OnInit } from '@angular/core';
import { Dater } from './dater.model'; // here i reference my date class
@Component({
  selector: 'app-date',
  templateUrl: './date.component.html',
  styleUrls: ['./date.component.css']
})
export class DateComponent implements OnInit {

  date: Dater; // assign property to type Date class
  constructor() { 
  }
  ngOnInit() {
  }

}
和我的视图文件
date.component.html

<p>
  date works!
  {{date.month}}
</p>

约会有效!
{{date.month}

我还想补充一点,我对OOP的理解还不是很好:)谢谢大家的帮助

更新代码后,我现在出现以下错误

无法读取未定义的属性'month'

有两个错误,您不应该将
关键字
作为类名,并且在指定时,将
与typescript一起使用

以下几点应该起作用

export class myClass {
    date = new Date();
    month: number = this.date.getMonth();
    day: number = this.date.getDate();
    constructor() {
    }

}

有两个错误,您不应该将
关键字
作为类名,并且在指定时将
与typescript一起使用

以下几点应该起作用

export class myClass {
    date = new Date();
    month: number = this.date.getMonth();
    day: number = this.date.getDate();
    constructor() {
    }

}

在组件的范围内,我们使用
this
引用属性或函数,即类上下文

export class Dater {

   date: Date = new Date();
   month: number = this.date.getMonth(); 
   day: number = this.date.getDate();

   constructor() {

   }

}`
在模板中,表达式上下文通常是组件实例(对象上下文),在本例中是DateComponent对象上下文

我们错过了declare实例,因此它将是未定义的

import { Component, OnInit } from '@angular/core';
import { Dater } from './dater.model'; // here i reference my date class
@Component({
  selector: 'app-date',
  templateUrl: './date.component.html',
  styleUrls: ['./date.component.css']
})
export class DateComponent implements OnInit {

  date: Dater = new Dater();
  constructor() { 
  }
  ngOnInit() {
  }

}

您可以参考此Angular 2文档,以进一步了解Angular 2上下文

在组件范围内,我们使用
来参考属性或函数,即类上下文

export class Dater {

   date: Date = new Date();
   month: number = this.date.getMonth(); 
   day: number = this.date.getDate();

   constructor() {

   }

}`
在模板中,表达式上下文通常是组件实例(对象上下文),在本例中是DateComponent对象上下文

我们错过了declare实例,因此它将是未定义的

import { Component, OnInit } from '@angular/core';
import { Dater } from './dater.model'; // here i reference my date class
@Component({
  selector: 'app-date',
  templateUrl: './date.component.html',
  styleUrls: ['./date.component.css']
})
export class DateComponent implements OnInit {

  date: Dater = new Dater();
  constructor() { 
  }
  ngOnInit() {
  }

}

您可以参考此Angular 2文档,以进一步了解Angular 2上下文

这纯粹是一个Typescript类实例化问题,其中模型中的Dater类未在DateComponent.ts中实例化 您可以在中了解有关Typescript类的更多信息


这将起到关键作用

这纯粹是一个Typescript类实例化问题,模型中的Dater类没有在DateComponent.ts中实例化 您可以在中了解有关Typescript类的更多信息



这将实现这个技巧

正如错误所说的那样使用
This.date.getMonth()
@suraj现在我收到以下错误
属性“getMonth”在键入日期时不存在
我看不到getMonth函数..我认为您有名称冲突..您正在使用相同的
日期创建一个类
name@suraj我正在尝试从本机javascript日期对象获取该函数。但是您的自定义模型类具有相同的名称..正如错误所示,use
this.date.getMonth()
@suraj现在我收到以下错误
属性“getMonth”在键入日期时不存在
我看不到getMonth函数..我认为您有名称冲突..您正在使用相同的
日期创建一个类
name@suraj我正在尝试从本机javascript日期对象获取该函数。但是您的自定义模型类具有相同的名称..我收到以下错误
无法读取未定义的属性'month'
为了使事情正常运行,我已将我的类名更改为
Dater
。我的视图仍然没有渲染像这样更新模板日期有效!{{date?.month}

已更新。但是没有呈现任何内容,也没有控制台错误。调试并查看是否分配了月份。我该如何做?我将把console.log语句放在哪里?我收到以下错误
无法读取未定义的属性'month'
为了使事情正常进行,我已将类名更改为
Dater
。我的视图仍然没有渲染像这样更新模板日期有效!{{date?.month}

已更新。但是没有呈现任何内容,也没有控制台错误。调试并查看是否分配了月份。我该如何做?我将把console.log语句放在哪里?谢谢!一切顺利。但是导入类并不能让我们访问它的属性???声明该实例似乎很奇怪。我没能理解的是,这是面向对象的,在声明instace(object)时,我们只能访问类的属性。除非我们将其设置为静态类型。谢谢!一切顺利。但是导入类并不能让我们访问它的属性???声明该实例似乎很奇怪。我没能理解的是,这是面向对象的,在声明instace(object)时,我们只能访问类的属性。除非我们将其设置为静态类型。