我们的一个项目涉及大量数据.它从数据库中选择数据并将结果序列化为JSON/XML.
有时,所选行的数量可以轻松达到5000万.
但是,程序的运行时间在一开始就很糟糕.
所以我们通过一项重大调整重构了该计划:
不会为每一行重新创建序列化的工作对象,而是清除并重新初始化该对象.
例如:
之前:
对于每个数据库行,我们创建DatabaseRowSerializer的对象并调用特定的序列化函数.
// Loop with all dbRows
{
DatabaseRowSerializer serializer(dbRow);
result.add(serializer.toXml());
}
Run Code Online (Sandbox Code Playgroud)
后:
DatabaseRowSerializer的构造函数不设置dbRow.相反,这将由initDbRow() - 函数完成.
这里最重要的是,只有一个对象将用于整个运行时.在序列化dbRow之后,将调用clear() - 函数来重置对象.
DatabaseRowSerializer serializer;
// Loop with all dbRows
{
serializier.initDbRow(dbRow);
result.add(serializer.toXml());
serializier.clear();
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题:
这真的是处理问题的好方法吗?在我看来,init() - 功能并不是很聪明.通常应该使用构造函数来初始化可能的参数.
您通常更喜欢哪种方式?之前还是之后?
有没有办法为动态对话框定义自定义标题模板?
对于普通对话框,您可以使用自定义 html 代码定义 p-header 标记。
但我没有找到任何方法来为动态对话框执行此操作。
如果用户未经授权(状态 == 401),我想显示 PrimeNG 动态对话框。但如果我想打开该对话框,则会出现错误:
NullInjectorError: No provider for DialogService!
Run Code Online (Sandbox Code Playgroud)
我通过将 DialogService 添加到根模块提供程序解决了这个问题:
@NgModule({
declarations: [],
imports: [],
entryComponents: [],
providers: [
DialogService
]
...
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否是正确的方法,因为正如官方文档中所写,每个组件都应该有自己的DialogService提供者。但是因为我需要其他服务(UnauthorizedHttpInterceptor)中的 DialogService,所以我必须将 DialogService 包含到根提供程序列表中。那是对的吗?
每个其他组件都有自己的提供程序:
@Component({
templateUrl: './dynamicdialogdemo.html',
providers: [DialogService]
})
Run Code Online (Sandbox Code Playgroud)
将使用他自己的实例。
那么我的解决方案应该没有问题吧?如果是这样,将 DialogService 实例提供给拦截器的正确方法是什么?
我的主要目的是避免多个 DialogService 组件的冲突,以便成功打开/关闭多个对话框。
从 v12 升级到 v13 后,我在尝试 ng 服务时收到以下消息。\n如果我使用 v13 设置一个全新的项目,一切正常。我已经尝试将配置文件(tsconfig,angular.json,...)与 vimdiff 进行比较,但没有发现任何大的差异。
\nError: node_modules/@angular/compiler-cli/src/ngtsc/file_system/src/compiler_host.d.ts:9:8 - error TS1259: Module '"/project/node_modules/typescript/lib/typescript"' can only be default-imported using the 'allowSyntheticDefaultImports' flag\n\n9 import ts from 'typescript';\n ~~\n\n node_modules/typescript/lib/typescript.d.ts:7478:1\n 7478 export = ts;\n ~~~~~~~~~~~~\n This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.\n\n\nError: node_modules/@angular/compiler-cli/src/ngtsc/file_system/src/logical.d.ts:9:8 - error TS1259: Module '"/project/node_modules/typescript/lib/typescript"' can only be default-imported using the 'allowSyntheticDefaultImports' flag\n\n9 import ts from 'typescript';\n ~~\n\n node_modules/typescript/lib/typescript.d.ts:7478:1\n …Run Code Online (Sandbox Code Playgroud) 我有以下 html 片段:
<div *ngIf="calculatedValue$ | async as calculatedValue">
{{calculatedValue}}
</div>
Run Code Online (Sandbox Code Playgroud)
但如果计算值 $ 的发出值为 0(类型 = 数字),则该 div 将不会显示在 DOM 中。
正确的展示方式是什么?也许是通过将非空检查添加到 *ngIf 的方式。
我想获取对象列表的函数结果的最小值。但是这个函数的返回值是可选的。所以如果到目前为止没有设置片段时间就可以了,然后返回值应该是Optional.empty()
public Optional<Double> getFragmentTime(int fragment) {
...
}
private List<Entry> entries; // will be filled in the ctor.
public Optional<Double> getMinFragmentTime(int fragment) {
return entries.stream()
.map(e -> e.getFragmentTime(fragment))
.filter(Optional::isPresent)
.map(Optional::get)
.min(Double::compare);
}
Run Code Online (Sandbox Code Playgroud)
这是存档的正确方法吗?这两个函数调用.filter(Optional.isPresent)和.map(Optional.get)对我来说似乎很奇怪,我认为必须有更好的解决方案。
在组件初始化期间,我想检索一些用户信息。现在,使用 Angular 严格模式,我不确定在哪里加载这些数据?
我能想到 3 个选项。但哪个是正确的呢?或者还有其他可能吗?
#1 在此选项中,将在 userInfo 初始化时直接调用服务方法。
export class NavComponent implements OnInit {
public userInfo$: Observable<LoggedInUser> = this.sessionService.getUserInformation();
constructor(private sessionService: SessionService) {}
ngOnInit(): void {}
}
Run Code Online (Sandbox Code Playgroud)
#2 在我看来,最好将其源代码到构造函数中,如下所示:
export class NavComponent implements OnInit {
public isMobileMenuCollapsed = true;
public userInfo$: Observable<LoggedInUser>;
constructor(private sessionService: SessionService) {
this.userInfo$ = this.sessionService.getUserInformation();
}
ngOnInit(): void {}
}
Run Code Online (Sandbox Code Playgroud)
#3 在 ngOnInit 方法中而不是构造函数。但是对于这个解决方案,我还需要明确分配断言:
export class NavComponent implements OnInit {
public isMobileMenuCollapsed = true;
public userInfo$!: Observable<LoggedInUser>;
constructor(private sessionService: SessionService) {
}
ngOnInit(): void …Run Code Online (Sandbox Code Playgroud) 我有一个动态大小的项目.对于我想要生成复选框的每个项目.我认为最好的方法是使用FormArray.
但我无法设置任何其他属性来指定动态复选框的标签.
items: Array<string> = ['Banana', 'Apple', 'Beer', 'Water'];
let checkboxArray = new FormArray([]);
this.items.forEach(item => {
let formControl = new FormControl(true);
checkboxArray.push(formControl);
})
Run Code Online (Sandbox Code Playgroud)
但正如我们在这里看到的,我只能指定formControl中复选框的值,但不能设置有关标签的任何其他信息.
一个工作的plunkr在这里:http://plnkr.co/edit/ODe5QSOEvQsQiuBzs56o?p=preview
angular ×5
primeng ×2
asynchronous ×1
c++ ×1
java ×1
java-8 ×1
java-stream ×1
optional ×1
typescript ×1