首页 > 解决方案 > 导航时丢失数据,页面更改

问题描述

我的应用程序显示一个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。谢谢!

标签: javascriptangulartypescriptionic-frameworkngmodel

解决方案


您是否确定:

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

这可以通过以下方式完成:在 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 {
}

推荐阅读