【发布时间】:2021-11-17 23:28:56
【问题描述】:
我有一个名为 getPuzzle() 的函数,它调用 API 来获取数据。我想使用该数据来修改 div。问题是这一切都是在构造函数中完成的,所以还没有加载html。
所以我把修改 html 的代码放到了 ionViewDidEnter 中,然后它在调用 API 之前运行,并且丢失了 this.puzzle 的数据
在 HTML 完全加载和 API 调用之后,我如何才能不运行此代码?
ngOnInit() {
this.getPuzzle();
}
getPuzzle() {
//Get puzzle info
this.puzzlesService.getPuzzle().subscribe((data) => {
this.puzzleType = this.puzzlesService.puzzleType;
this.puzzle = this.puzzlesService.puzzle;
});
}
ionViewDidEnter() {
//Set first tab
if (this.puzzleType == 'text') {
new Vara("#text", "assets/fonts/vara/Satisfy/SatisfySL.json", [{
text: this.puzzle,
delay: 2000,
x: 2
}], {
fontSize: 25,
strokeWidth: 1.5,
duration: 15000,
color: "black"
});
}
}
【问题讨论】:
-
在 API 调用中运行它。
this.puzzle = this.puzzlesService.puzzle; });在这行之后。 -
当时没有加载 HTML,无法访问#text id
-
你为什么订阅
puzzlesService.getPuzzle(),然后忽略数据?好像很腥!另外,为什么这个组件会复制您有权访问的服务的至少 3 个属性?getPuzzle、puzzleType和puzzle存在于该组件和拼图服务中。这是有原因的吗?
标签: javascript angular typescript ionic-framework rxjs