我在网上的一篇文章中读到了这句话:
向 1000 个单元中的每一个添加事件处理程序将是一个主要的性能问题,并且可能是浏览器崩溃内存泄漏的来源。
但我不明白。例如,如果我正在做这样的事情:
const handler = e => // do something
document.querySelectorAll("td").forEach(td => {
td.addEventListener("click", handler);
});
Run Code Online (Sandbox Code Playgroud)
我没有创建 1000 个函数,它们都使用相同的引用,那么我缺少什么?问题是什么?
我很困惑。style()a 里面的函数有什么区别state()
state('inactive', style({
backgroundColor: '#eee',
transform: 'scale(1)'
})),
Run Code Online (Sandbox Code Playgroud)
和style()里面的一个函数transition()
transition('inactive => *', [
style({ transform: 'scale3d(.3, .3, .3)' }),
animate(100)
]),
Run Code Online (Sandbox Code Playgroud)
和style()里面的一个函数animation()
animate(100, style({ transform: 'scale3d(.0, .0, .0)' }))
Run Code Online (Sandbox Code Playgroud) 我有一个需要公开提供程序的延迟加载模块,所以我使用了forRoot约定并返回以下代码:
@NgModule({
imports: [RouterModule.forChild([
{path: "", component: LazyComponent},
])],
declarations: [LazyComponent],
})
export class LazyModule {
static forRoot() {
return {
ngModule: LazyModule,
providers: [provider]
};
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我在我的应用程序模块中调用 forRoot 时,延迟加载不再起作用。(我在控制台中没有看到单独的块)
@NgModule({
declarations: [
AppComponent,
HelloComponent
],
imports: [
BrowserModule,
AppRoutingModule,
LazyModule.forRoot() <======== this stops the lazy load module
],
bootstrap: [AppComponent]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)
据我所知,它应该只使提供者成为单例,为什么它不起作用?
我有一个使用startWith运算符的流debounceTime.我希望第一个值跳过debounceTime并立即启动.我该怎么做?
control.valueChanges
.pipe(
startWith(control.value), <=== this needs to skip debounce
debounceTime(200),
map(...),
);
Run Code Online (Sandbox Code Playgroud) 我有一个要转换为对象的数组。例如:
const arr = [{id: 1, key: ''}, {id: 2, key: ''}];
Run Code Online (Sandbox Code Playgroud)
我希望结果是:
const object = { 1: {id: 1, key: ''}, 2: { id: 2, key: ''}}
Run Code Online (Sandbox Code Playgroud)
随着lodash我可以使用的keyBy功能,但我有工作ramda并没有在那里找到此功能。
我想使用带有业力的 ChromeHeadless。我的配置是:
process.env.CHROME_BIN = require('puppeteer').executablePath();
module.exports = function( config ) {
config.set({
basePath : '',
frameworks : ['jasmine', '@angular/cli'],
plugins : [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('@angular/cli/plugins/karma')
],
angularCli : {
environment: 'dev'
},
reporters : ['progress'],
autoWatch : false,
browsers: [
'ChromeHeadless',
],
singleRun : true
});
};
Run Code Online (Sandbox Code Playgroud)
当我运行 ng test 时,我仍然需要打开浏览器。这是我从业力控制台得到的信息:
Karma v2.0.0 服务器启动于http://0.0.0.0:9876/
我有一个根据选项键值返回不同类型的方法。
class Test {
getData(options: { obj: true }): Object;
getData(options: { obj: false }): any[];
getData(): any[];
getData(options = { obj: false }): Object | any[] {
if (options.obj) {
return {};
} else {
return [];
}
}
}
Run Code Online (Sandbox Code Playgroud)
当传递objas 时true,我返回对象否则数组。这很好用。
const instance = new Test();
const result = instance.getData({ obj: true }); // inffered as array
const result2 = instance.getData(); // inffered as object
Run Code Online (Sandbox Code Playgroud)
问题是当我需要使用动态值时,它会引发错误:
类型 boolean 不可分配给类型 false
function getResult(obj: boolean = …Run Code Online (Sandbox Code Playgroud) 我有以下get功能:
function get<T>(obj: T, props: (keyof T)[] | keyof T): any {
const toArray = coereceArray(props);
return obj && toArray.reduce(
(result, prop) => result == null ? undefined : result[prop] as any,
obj
);
}
const result = get({ a: { b: 'hey' } }, ['a', 'b']);
const result2 = get({ a: { b: 'hey' } }, 'a');
Run Code Online (Sandbox Code Playgroud)
如何根据传递的参数动态输入结果?
我有一个接受未知对象的函数,我想输出它们的合并版本:
export function merge(...objects: any[]) {
return objects.reduce((acc, current) => {
acc = {
...acc,
...current
}
return acc;
}, {});
}
const a = { name: '' };
const b = { id: 1 };
const c = { prop: '' };
const result = merge(a, b, c);
Run Code Online (Sandbox Code Playgroud)
如何result根据传递的对象自动推断变量的类型?
我想将一个类传递给一个函数并推断该类的键,但它不起作用:
class Foo {
bar: string;
}
function foo<C>(comp: C, key: keyof C) {
}
foo(Foo, '') // expecting to get bar here, but I'm getting the prototype key
Run Code Online (Sandbox Code Playgroud)
当我将它用作类型时,它可以工作:
type Keys = keyof Foo;
Run Code Online (Sandbox Code Playgroud)
有什么问题吗?
我有一个构造函数,它应该根据参数返回不同的类型。
interface B {
hello(): string;
}
class Foo {
constructor(withB: boolean): Foo & B;
constructor();
constructor(withB?: boolean) {
if (withB) {
Object.assign(this, { hello() {}})
}
}
}
const foo1 = new Foo();
const foo3 = new Foo(true);
Run Code Online (Sandbox Code Playgroud)
但它不起作用。我该怎么做?