javascript - 导航时丢失数据,页面更改
问题描述
我的应用程序显示一个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。谢谢!
解决方案
您是否确定:
- 主页和编辑页面都提供服务?
这可以通过以下方式完成:在 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 {
}
- 主页和编辑页面之间的导航使用 Angular 路由器:https ://angular.io/guide/router
推荐阅读
- android - 领域一对多关系不持久
- php - PHP错误显示MySQL服务器已消失错误
- node.js - 在数组中查找元素告诉我参数必须是对象
- scala - 如何读取带有可变分隔符的 ASCII 文件?
- c++ - C++ 指针危险?如何为 Arduino 成功创建更通用的 i2c/TWI 库?
- php - 按匹配字符串的字段排序
- python-3.x - 如何将 itertools 用于嵌套循环?
- java - WSO2 身份服务器无法使用自定义联合身份验证器
- c++ - 如果 C++ 应用程序与模板重的库链接,是否在编译应用程序时重新编译库中的所有模板定义?
- wordpress - Wordpress.org 在此服务器上找不到请求的 URL