我遇到过几个不同的代码实例,其中使用显式类型声明变量,即使推断类型很明显:
示例:loading: boolean = false
或name: string = "John"
或count: number = 0
等。
TSLint 更喜欢推断类型而不是显式类型,所以我想知道这只是一个文体问题吗?这些显式类型在运行时是否重要?
Plunkr:http://plnkr.co/edit/y4e4jS89gOxRKQOyUW2r?p =preview
我在mat-chip上使用selectionChange @Output来查看芯片选择的结果,但似乎eventEmitter没有触发芯片选择?
html的:
<mat-chip-list>
<mat-chip (selectionChange)="changeSelected($event)">Papadum</mat-chip>
<mat-chip (selectionChange)="changeSelected($event)">Naan</mat-chip>
<mat-chip (selectionChange)="changeSelected($event)">Dal</mat-chip>
</mat-chip-list>
<p>Currently Selected: {{selected}}</p>
Run Code Online (Sandbox Code Playgroud)
.TS:
selected: string;
changeSelected(e) {
console.log(e);
this.selected = e.value;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,单击选择时根本不会发出任何事件.这是一个仍在开发中的东西,还是选择意味着与我的想法不同?
Firebase文档建议明智地管理依赖关系,这是减少冷启动时间的第一件事:https : //firebase.google.com/docs/functions/tips#use_dependencies_wisely
对于大多数Firebase功能,您可能会使用firebase-functions
和firebase-admin
模块。
这是一个实用的示例函数:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
// Initialize the app in admin sdk
admin.initializeApp();
// Firestore timestamp support
admin.firestore().settings({timestampsInSnapshots: true});
exports.simpleFunction = functions.https.onCall((data, context) => {
return admin.auth().getUserByEmail(data.email)
.catch(() => {
return Promise.reject(new https.HttpsError('not-found', 'User not found.'));
})
.then(userRecord => {
return admin.firestore().collection('user').doc(userRecord.uid).get();
})
});
Run Code Online (Sandbox Code Playgroud)
冷启动执行时间:Function execution took 7517 ms, finished with status code: 200
热启动时间:Function execution took 8 ms, finished with status code: 200
如图所示,对该功能的测试具有〜7.5秒的冷启动启动时间(尽管显着的热响应时间)。尽管有许多因素可以解释为什么会出现这种情况,但是一个原因可能是加载 …
如何存根/模拟读取为 的指令/组件ViewChild
?
例如,使用 angular.io 中的简单指令:
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor() { }
}
Run Code Online (Sandbox Code Playgroud)
假设我正在测试并将使用AppComponent
读取为:HighlightDirective
ViewChild
@ViewChild(HighlightDirective) theHighlightDirective: HighlightDirective
Run Code Online (Sandbox Code Playgroud)
存根指令是:
@Directive({
selector: '[appHighlight]'
})
export class StubbedHighlightDirective {
constructor() { }
}
Run Code Online (Sandbox Code Playgroud)
由于组件正在尝试读取,即使您在单元测试中HighlightDirective
声明,也将是。StubbedHighlightDirective
theHighlightDirective
undefined
例子:
it('HighlightDirective is defined', () => {
// This test fails
expect(component.theHighlightDirective).toBeDefined();
});
Run Code Online (Sandbox Code Playgroud)
如果您忽略 tslint 中的某些内容或使用as
关键字,则可以解决此问题:
Version 1: Just ignore some things in tslint so compiler doesn't complain
it('HighlightDirective is defined', () …
Run Code Online (Sandbox Code Playgroud) angular ×3
firebase ×1
jasmine ×1
javascript ×1
node.js ×1
typescript ×1
unit-testing ×1
viewchild ×1