【问题标题】:Loosing data upon navigation, page change导航时丢失数据,页面更改
【发布时间】:2019-09-01 16:16:23
【问题描述】:

我的应用显示一个可以编辑的profile name。当我编辑它时,它会改变,太好了。当我导航并返回时,数据会重置。

我尝试将数据(配置文件名称)保存为 String 变量,我尝试将数据保存为列表并使用 String[0] 显示名称以显示未移位的配置文件名称。

先去

<h3>{{profileService.profileName}}'s Profile</h3>

this.profileService.changeName(this.pName);

changeName(data){
    this.profileName = data;
}

第二次去

<h3>{{profileService.profileNames[0]}}'s Profile</h3>

this.profileService.changeName(this.pName);

changeName(data){
    this.profileNames.unshift(data);
}

所以,当我从编辑页面转到主页时,这个更新最初会更新。当我转到另一个页面并返回时,更新后的个人资料名称为 MISSING。谢谢!

【问题讨论】:

  • 是否可以在 stackblitz.com/ 上创建项目并分享链接?这样发生的事情会更清楚。
  • 我重建了它,它现在可以工作了,但是谢谢。

标签: javascript angular typescript ionic-framework ngmodel


【解决方案1】:

您是否确定:

  • 首页和编辑页面都提供服务?

这可以通过以下方式完成: 在 app.module.ts 中(我想您希望您的更改在整个应用程序中持续存在)

@NgModule({
  declarations:[
  //your components here
  ],
  imports:[
  //your modules here 
  ],
  providers:[
  ProfileService //and other services
  ]
})

或在 profile.service.ts 中

import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root'// which means there will be a single instance of the service in the whole application
})
export class ProfileService {
}

【讨论】:

  • 感谢您的思考,这些条件不是错误。我想它们是随版本一起提供的。
猜你喜欢
  • 1970-01-01
  • 2022-09-28
  • 2019-08-24
  • 2013-12-10
  • 2020-12-13
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多