Tag: 打字稿

如何在TypeScript中创build枚举types?

我正在为TypeScript的Google maps API定义文件。 我需要定义一个像枚举types例如。 包含两个属性的google.maps.Animation : BOUNCE和DROP 。 在TypeScript中应该怎么做?

如何在TypeScript中声明一个types为空?

我在TypeScript中有一个接口。 interface Employee{ id: number; name: string; salary: number; } 我想把“工资”作为一个可空字段(就像我们在C#中可以做的那样)。 这可能在TypeScript中做到吗?

Injectable类实例化时不会调用ngOnInit

当Injectable类被parsing时为什么不ngOnInit() ? 码 import {Injectable, OnInit} from 'angular2/core'; import { RestApiService, RestRequest } from './rest-api.service'; @Injectable() export class MovieDbService implements OnInit { constructor(private _movieDbRest: RestApiService){ window.console.log('FROM constructor()'); } ngOnInit() { window.console.log('FROM ngOnInit()'); } } 控制台输出 FROM constructor()

如何获得TypeScript枚举条目的名称?

我想知道如何迭代一个TypeScript枚举和每个枚举的符号名称。 例如, enum myEnum { entry1, entry2 } for (var entry in myEnum) { // use entry's name here, eg, "entry1" }

如何扩展/inheritanceAngular2组件?

题 我想为已经部署在Angular 2中的一些组件创build扩展,而不必几乎完全重写它们,因为基本组件可能发生变化,并希望这些变化也反映在派生的组件中。 例 我创build了这个简单的例子,试图解释更好的问题: 使用以下基本组件app/base-panel.component.ts : import {Component, Input} from 'angular2/core'; @Component({ selector: 'base-panel', template: '<div class="panel" [style.background-color]="color" (click)="onClick($event)">{{content}}</div>', styles: [` .panel{ padding: 50px; } `] }) export class BasePanelComponent { @Input() content: string; color: string = "red"; onClick(event){ console.log("Click color: " + this.color); } } 您是否想创build另一个派生组件,例如,在示例颜色app/my-panel.component.ts的情况下,基本组件行为: import {Component} from 'angular2/core'; import {BasePanelComponent} from […]

Angular 2unit testing:找不到名字'describe'

我正在从angular.io继续这个教程 正如他们所说,我已经创build了hero.spec.ts文件来创buildunit testing: import { Hero } from './hero'; describe('Hero', () => { it('has name', () => { let hero: Hero = {id: 1, name: 'Super Cat'}; expect(hero.name).toEqual('Super Cat'); }); it('has id', () => { let hero: Hero = {id: 1, name: 'Super Cat'}; expect(hero.id).toEqual(1); }); }); unit testing就像一个魅力。 问题是:我看到一些错误,在教程中提到: 我们的编辑和编译器可能会抱怨说,他们不知道it和expect是什么it因为他们缺乏描述茉莉花的打字文件。 现在我们可以无视那些烦人的投诉,因为它们是无害的。 他们确实忽视了它。 即使这些错误是无害的,当我收到他们一堆时,在我的输出控制台中看起来不太好。 我得到的例子: […]

如何在TypeScript中传递可选参数,同时忽略其他一些可选参数?

鉴于以下签名: export interface INotificationService { error(message: string, title?: string, autoHideAfter? : number); } 我怎样才能调用函数error() 不指定标题参数,但设置autoHideAfter说1000?

“HTMLElement”types的值不存在属性“值”

我正在打字打字,并试图创build一个脚本,将更新一个p元素作为文本input框中input。 该html看起来如下所示: <html> <head> </head> <body> <p id="greet"></p> <form> <input id="name" type="text" name="name" value="" onkeyup="greet('name')" /> </form> </body> <script src="greeter.js"></script> </html> 和greeter.ts文件: function greeter(person) { return "Hello, " + person; } function greet(elementId) { var inputValue = document.getElementById(elementId).value; if (inputValue.trim() == "") inputValue = "World"; document.getElementById("greet").innerText = greeter(inputValue); } 当我与tsc编译时,我得到以下“错误”: /home/bjarkef/sandbox/greeter.ts(8,53): The property 'value' does […]

将angular度redirect到login页面

我来自Asp.Net MVC世界,用户试图访问他们没有被授权的页面会自动redirect到login页面。 我正试图在Angular上重现这种行为。 我来到了@CanActivate装饰器,但它导致组件根本没有渲染,没有redirect。 我的问题如下: Angular是否提供了实现这种行为的方法? 如果是这样,怎么样? 这是一个很好的做法吗? 如果没有,那么在Angular中处理用户授权的最佳做法是什么?

Typescript是否支持? 运营商? (那叫什么?)

Typescript目前(或有计划)是否支持?的安全导航操作符?. 即: var thing = foo?.bar // same as: var thing = (foo) ? foo.bar : null; 另外,这个操作符有没有更常见的名称(这对谷歌来说是非常困难的)。