【发布时间】:2021-02-22 09:03:37
【问题描述】:
我正在处理一个错误,当我尝试创建新的 page 对象时,它会发送到后端但它没有更新数组,我需要重新加载页面以查看所有数组。
我在前端的async 中使用 Observable。
我尝试控制台记录page.component.ts 的ngOnInit,但是当我添加新页面并导航到页面时,ngOnInit 没有调用。
在创建新页面时会发生这种情况。
它会将我发送到pages 的路线,在那里我会显示所有页面列表。
但是当我创建新的Page 时,它会返回一个错误,上面写着。
ERROR Error: Error trying to diff 'Here is the name of the object'. Only arrays and iterables are allowed.
更新:正如 Marco 所说,发生这种情况是因为我将页面混合为对象而不是遍历数组
但我无法解决它,我需要你的帮助。
在pageModel 的page.service.ts 中,当我添加新对象时,它只返回添加的Object,而不是整个数组,我认为存在问题,但我不知道如何解决。
但是如果我重新加载页面,那么我会看到我所有的数组。
这是我更新的代码。
这是我的代码。
export class PagesService {
public baseUrl = environment.backend;
private data = new ReplaySubject<any>();
public userID = this.authService.userID;
public editDataDetails: any = [];
public subject = new Subject<any>();
private messageSource = new BehaviorSubject(this.editDataDetails);
getPageID = this.messageSource.asObservable();
constructor(private http: HttpClient, private authService: AuthService) { }
public getPages() {
return this.http.get<any>(`${this.baseUrl}/pages/${this.userID}`).subscribe(res => this.data.next(res));
}
public pageModel(): Observable<Page[]> {
return this.data.asObservable(); // Here it throws error
}
public getPage(id): Observable<any> {
return this.http.get(`${this.baseUrl}/page/${id}`);
}
public setPage(page: Page, id: string) {
const api = `${this.baseUrl}/page`;
const user_id = id;
this.http.post<any>(api, page, {
headers: { user_id }
}).subscribe(res => this.data.next(res));
}
changeMessage(message: string) {
this.messageSource.next(message)
}
public updateDate(id: string, page: Page) {
const api = `${this.baseUrl}/page/${id}`;
return this.http.put<any>(api, page).subscribe(res => this.data.next(res.data));
}
来自答案的更新代码。
public updateDate(id: string, page: Page) {
const api = `${this.baseUrl}/page/${id}`;
return this.http.put<any>(api, page).subscribe(res => {
this.lastSetOfData = res;
this.data.next(this.lastSetOfData);
});
}
}
export class Page {
_id = "";
name = "";
slogan = "";
description = "";
url = "";
telephone: number;
pageUrl: string;
website: string;
founded: number;
organization: number;
email: string;
coverImage: string;
profileImage: string;
specialty?: Specialty[];
branches: Branches[];
locations?: Location[];
phone?:Phone;
userRole?: string;
roles?: Roles[];
}
export class Roles {
role= "";
userID = "";
}
这是 page.component 的 HTML。
<div class="main" *ngIf="!showWeb">
<div *ngFor="let page of pages$ | async" class="card width-900">
<app-pages-list class="d-flex width-900" [page]="page" [details]="'details'"></app-pages-list>
</div>
<div>
</div>
</div>
这是 TS 文件。
public pages$: Observable<Page[]>;
ngOnInit(): void {
this.pageService.getPages();
this.pages$ = this.pageService.pageModel();
}
这是我创建新页面时的代码。
export class CreatePageComponent implements OnInit {
public page = new Page();
search;
public branch = [];
constructor(public router: Router,
public branchesService: BranchesService,
public authService: AuthService,
public pageService: PagesService,
public shareData: SenderService) { }
ngOnInit(): void {
}
createPage() {
this.page.url = this.page.name;
this.page.branches = this.branch;
this.page.locations = [];
this.page.specialty = [];
this.page.roles = [];
this.page.phone = this.page.phone;
this.page.pageUrl = `${this.page.name.replace(/\s/g, "")}${"-Page"}${Math.floor(Math.random() * 1000000000)}`;
this.pageService.setPage(this.page, this.authService.userID);
}
addBranch(event) {
this.branch.push(event);
this.search = "";
}
removeBranch(index) {
this.branch.splice(index, 1);
}
}
【问题讨论】:
-
请改进您的问题,因为它很难清楚地理解。您的 HTML 文件中是否有打字稿代码?您有一个从未使用过且从未声明过的
pages变量。其他变量从不声明。 -
我认为问题在于您使用空数组初始化数组。然后,您将项目添加到该数组。角度变化检测通过比较实例来检查他是否需要重新渲染。如果它是同一个数组的实例,它不会触发重新渲染。我不确定是否是问题所在,因为我不知道去哪里找。
-
@Marco 我已经编辑了我的代码并删除了未使用的代码。
-
@Marco 我已经编辑了我的问题,添加了其他返回一些错误的代码。
-
如果您在服务上正确使用了类型,您可能不会遇到这个问题。现在一切都是
any,并且您将对象放在需要数组的位置。
标签: javascript angular typescript rxjs observable