基本角度2打字稿服务设置

时间:2016-06-24 16:39:43

标签: typescript angular

我开始将Angular 1项目转换为Angular 2,并且在设置第一个服务时遇到了一些问题。我设置了一个与我试图做的相匹配的傻瓜,它完全被轰炸了。这就是傻瓜:

https://plnkr.co/edit/oYrg68HhTihPFAcSGU8t?p=preview

在实际应用程序中,我使用了角度CLI生成器并收到以下错误:

  

错误:Typescript发现以下错误:
  /path/to/project/tmp/broccoli_type_script_compiler-input_base_path-bvDJA9MD.tmp/0/src/app/+login/login.component.spec.ts(17,21):提供的参数与任何呼叫签名都不匹配   目标

如果那里的任何人都可以帮助我让傻瓜工作,那就太棒了!

这是规范文件,如果它有用:

/* tslint:disable:no-unused-variable */

import { By }           from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import {
  beforeEach, beforeEachProviders,
  describe, xdescribe,
  expect, it, xit,
  async, inject
} from '@angular/core/testing';

import { LoginComponent } from './login.component';

describe('Component: Login', () => {
  it('should create an instance', () => {
    let component = new LoginComponent();
    expect(component).toBeTruthy();
  });
});

1 个答案:

答案 0 :(得分:0)

Here's你的plunkr修改版本正在为我工​​作。

似乎您需要提供TestService,然后将其作为参数传递给构造函数。

更新了app.ts。

//our root app component
import {Component} from '@angular/core';
import {TestService} from './test.service';

@Component({
  selector: 'my-app',
  providers: [ TestService  ],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <p><a (click)="logIn('test-user', '123456')">Test Login</a></p>
      <p>{{status}}</p>
    </div>
  `,
  directives: []
})
export class App {

  constructor(
    private testService:TestService;) {
    this.name = 'Pizza';
    this.status = 'Sword';

  }

  logIn(username, password) { 
    this.status = this.testService.logIn(username, password);
  }
}
相关问题