gpt4 book ai didi

angular - TS1086 : An accessor cannot be declared in ambient context

转载 作者:行者123 更新时间:2023-12-02 03:11:18 30 4
gpt4 key购买 nike

在初始实现后,我在终端 ng serve my-app of 中输入了数千个此错误,但我无法解决它。当我在 typescript 的 Angular 内遇到这样的问题时,这是我第一次
错误如下所示:

ERROR in ../../node_modules/@angular/flex-layout/core/typings/base/base2.d.ts:24:19 - error TS1086: An accessor cannot be declared in an ambient context.

24     protected get parentElement(): HTMLElement | null;
~~~~~~~~~~~~~
../../node_modules/@angular/flex-layout/core/typings/base/base2.d.ts:26:19

- error TS1086: An accessor cannot be declared in an ambient context.

26     protected get nativeElement(): HTMLElement;
~~~~~~~~~~~~~
../../node_modules/@angular/flex-layout/core/typings/base/base2.d.ts:28:9

- error TS1086: An accessor cannot be declared in an ambient context.

28     get activatedValue(): string;
~~~~~~~~~~~~~~
../../node_modules/@angular/flex-layout/core/typings/base/base2.d.ts:29:9

- error TS1086: An accessor cannot be declared in an ambient context.

29     set activatedValue(value: string);
~~~~~~~~~~~~~~
../../node_modules/@angular/flex-layout/core/typings/breakpoints/break-point-registry.d.ts:20:9

- error TS1086: An accessor cannot be declared in an ambient context. [...]



有人知道原因吗?在我修复它之前,我无法测试我的应用程序。

更新 1

好的,我继续前进。大多数错误都消失了,但我现在有几个错误,例如第一个:

ERROR in src/app/main/main.component.ts:143:63 - error TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.

143 this.fileService.add({ isFolder: true, name: folder.name, parent: this.currentRoot ? this.currentRoot.id : 'root' });



代码如下所示:

main.component.ts:
currentRoot: MpFileElement = new MpFileElement();
...
addFolder(folder: { name: string }) {
this.fileService.add({ isFolder: true, name: folder.name, parent:
this.currentRoot ? this.currentRoot.id : 'root' });
this.updateFileElementQuery();
}
...

文件.service.ts:
import { Injectable } from '@angular/core';

import { v4 } from 'uuid';
import { MpFileElement } from '../models/mp-file-element.model';
import { Observable } from 'rxjs/internal/Observable';
import { BehaviorSubject } from 'rxjs';

export interface IFileService {
add(fileElement: MpFileElement);
delete(id: string);
update(id: string, update: Partial<MpFileElement>);
queryInFolder(folderId: string): Observable<MpFileElement[]>;
get(id: string): MpFileElement;
}

@Injectable()
export class MpFileService implements IFileService {

constructor() {}
private map = new Map<string, MpFileElement>()

private querySubject: BehaviorSubject<MpFileElement[]>;

add(fileElement: MpFileElement) {
fileElement.id = v4();
this.map.set(fileElement.id, this.clone(fileElement));
return fileElement;
}

delete(id: string) {
this.map.delete(id);
}

update(id: string, update: Partial<MpFileElement>) {
let element = this.map.get(id);
element = Object.assign(element, update);
this.map.set(element.id, element);
}
queryInFolder(folderId: string) {
const result: MpFileElement[] = [];
this.map.forEach(element => {
if (element.parent === folderId) {
result.push(this.clone(element));
}
})
if (!this.querySubject) {
this.querySubject = new BehaviorSubject(result);
} else {
this.querySubject.next(result);
}
return this.querySubject.asObservable();
}

get(id: string) {
return this.map.get(id);
}

clone(element: MpFileElement) {
return JSON.parse(JSON.stringify(element));
}
}

最佳答案

"skipLibCheck": true 中设置 tsconfig.json 解决了我的问题

"compilerOptions": {
"skipLibCheck": true
}

关于angular - TS1086 : An accessor cannot be declared in ambient context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60092642/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com