如何在打字稿中调用函数

问题描述 投票:0回答:2

我试图通过我的模板click事件调用函数,但错误是“获取不是一个函数”。有人能找到问题所在吗?

我的模板:

    <button class="btn btn-primary" (click)="get()">sign-up</button>

我的:

import {Component} from '@angular/core';
import {FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES} from '@angular/forms';
import {FormBuilder, FormGroup, Validators} from '@angular/forms'
import { Http, Headers } from '@angular/http';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
@Component({
  directives: [FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES],
     templateUrl : "./components/login/login.html",
   })
      export class Login {
   //http: Http;           
     form: FormGroup;    

   constructor(public router: Router,  public http: Http, fbld:    FormBuilder) {
   // this.http = http;
    this.form = fbld.group({
        email: ['', Validators.required],
        phone: ['', Validators.required]
     });

  }
   function get(){
  //  this.router.navigate(['/demo/signup']);
    alert('hai');
  }
html angularjs typescript angular typescript1.8
2个回答
1
投票

只要您正在使用Typescript,就不需要使用function关键字定义函数。

所以改成它,

get(){
   ...
}

1
投票

在typescript中,方法声明如下:

public get() {
   ....
}

你的函数不起作用,因为像这样的声明是javascript对象的内部函数。要使用like函数,请执行以下操作:

public get = function() {
    ....
}
© www.soinside.com 2019 - 2024. All rights reserved.