ts无法在角度4

时间:2017-08-01 06:05:44

标签: angular

我是角度4的新手,我的错误是[ts] Cannot find name 'model'  我的角度4项目。请帮助我,我的错误在哪里。  提前谢谢。

import { Component, OnInit } from '@angular/core';
import { Customer }    from './customer';

@Component({
  selector: 'app-customer-profile',
  templateUrl: './customer-profile.component.html',
  styleUrls: ['./customer-profile.component.css']
})
export class CustomerProfileComponent implements OnInit {

  constructor() {
   }

  ngOnInit() {
   model = new Customer(1,'vikram','R',25);
  }

}

2 个答案:

答案 0 :(得分:0)

  

试试这段代码

import { Component, OnInit } from '@angular/core';
import { Customer }    from './customer';

@Component({
  selector: 'app-customer-profile',
  templateUrl: './customer-profile.component.html',
  styleUrls: ['./customer-profile.component.css']
})
export class CustomerProfileComponent implements OnInit {

  constructor() {
   }
  model:Customer;
  ngOnInit() {
   this.model = new Customer(1,'vikram','R',25);
  }

}

您需要在使用之前声明变量。

答案 1 :(得分:0)

您需要声明您在打字稿中使用的变量。

如果model是类变量,请执行:

export class CustomerProfileComponent implements OnInit {

model:Customer;//declare here
  constructor() {
   }

  ngOnInit() {
   this.model = new Customer(1,'vikram','R',25);//use this to access
  }

}

或者如果它是局部变量,

 ngOnInit() {
  let model:Customer = new Customer(1,'vikram','R',25);//use let to access
  }