首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nativescript在应用程序启动时更新http响应

Nativescript在应用程序启动时更新http响应
EN

Stack Overflow用户
提问于 2018-11-06 20:03:33
回答 2查看 146关注 0票数 0

我继承了一个应用程序,它从API端点获取数据。我们发现,当我们更改API上的数据时,这些更改没有反映在应用程序中。如果我们卸载并重新安装移动设备上的应用程序,则会显示来自API的新数据。下面是“建筑物详细信息”页面的一个示例:

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { switchMap } from 'rxjs/operators';
import { Building } from "../shared/building/building";
import { HttpService } from "../services/http/http.service";
import {
    getString,
    setString
} from "application-settings";

@Component({
    moduleId: module.id,
    selector: 'building-detail',
    templateUrl: 'building-detail.component.html',
    styleUrls: ["./building-detail-common.css"],
    providers: [ Building, HttpService ]
})

export class BuildingDetailComponent implements OnInit {
    paramName: string;
    constructor(
        private route: ActivatedRoute, 
        public building: Building, 
        private httpService: HttpService) {
        this.route.params.subscribe(
            (params) => {
                this.paramName = params['name']
            } 
        );
    }

    ngOnInit() {
        console.log("ON INIT FIRED " + this.paramName);
        let buildingInfo = JSON.parse(getString("buildingInfo"));
        for (let item of buildingInfo) {
            if (item.attributes.title === this.paramName) {
                this.building.name = item.attributes.title;
                this.building.desc = item.attributes.body.value;
                let imageEndpoint = "file/file/" + item.relationships.field_building_image.data.id;
                let imageUrl = this.httpService.getData(imageEndpoint)
                .subscribe(data => { 
                    this.building.image = "https://nav.abtech.edu" + data['data'].attributes.url;
                    console.log("The building image URL is " + this.building.image);
                }, (error) => {
                    console.log("Error is " + error);
                });
            }
        }

     }
}

我很高兴分享其他文件/代码,如果你想看看这些。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-14 20:58:36

您的数据没有被更新的原因不是因为ngOnInit没有被执行,而是因为您在每次运行应用程序时都缓存旧值并重新加载它。您正在使用appSettings持久地缓存整个应用程序运行的数据,这就是为什么在卸载之前这些值保持不变的原因。

如果您不想显示缓存的值,那么就不要从应用程序设置中读取,或者至少在刷新一次数据之前不要从appSettings读取。

票数 1
EN

Stack Overflow用户

发布于 2018-11-07 16:16:08

ngOnInit是只在创建组件时才会执行的东西,它再也不会被执行了。

此外,应用启动和恢复也有区别,如果您希望每次用户打开应用程序时都更新数据,则应该在ngZone中侦听恢复事件并执行api调用。

如果要在后端数据更改时立即通知用户,甚至可以使用推送通知/数据消息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53179182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档