angular2 在组件、getter 和 setter 之间共享数据
angular2 sharing data between components , getters and setters
我有一个与这里的人相同的问题:
我认为提供的解决方案不足以让我的项目运行,所以我将详细说明我的情况,看看你们是否可以帮助我!
我有一个服务 dataBase.service.ts :
import {Injectable} from "@angular/core";
@Injectable()
export class dataBaseService{
serviceData: string;
get data():string{
return this.serviceData;
}
set data(value:string){
this.serviceData = value;
}
}
此服务显然已作为提供商添加到 app.module.ts..
在我的组件 A 上我有:
import { Component, OnInit } from '@angular/core';
import {dataBaseService} from "../dataBase.service";
@Component({
selector: 'app-tipo',
templateUrl: './tipo.component.html',
styleUrls: ['./tipo.component.scss'],
providers: [dataBaseService]
})
export class A implements OnInit {
constructor( public dataService:dataBaseService) {
this.dataService.serviceData = 'hello';
}
ngOnInit() {
console.log(this.dataService.serviceData);
}
}
直到这里一切都很好。如果我在控制台上显示:
console.log(this.dataService.serviceData);
它returns我"hello"如预期
但是在我的下一个组件上,当我再次打印相同的数据时,它显示未定义:
import { Component, OnInit } from '@angular/core';
import {dataBaseService} from "../dataBase.service";
@Component({
selector: 'app-modelo',
templateUrl: './modelo.component.html',
styleUrls: ['./modelo.component.scss'],
providers: [dataBaseService]
})
export class ModeloComponent implements OnInit {
ngOnInit() {
console.log(this.dataService.serviceData);
}
constructor(public dataService: dataBaseService) {
}
}
console.log(this.dataService.serviceData);
它returns"undefined"..
那么我如何保存我放在第一个组件上的数据并能够在另一个组件上显示它呢?我错过了什么?
更新:
像我这样的一些人在堆栈上找不到像这个问题这样的另一个问题的答案,那是因为您必须全局添加它们(在 module.ts 上),而不是单独添加提供者! !!!!!!!!!
正如您所说,您已经在模块级别添加了提供者,请尝试从组件中注释掉提供者声明。
我有一个与这里的人相同的问题:
我认为提供的解决方案不足以让我的项目运行,所以我将详细说明我的情况,看看你们是否可以帮助我!
我有一个服务 dataBase.service.ts :
import {Injectable} from "@angular/core";
@Injectable()
export class dataBaseService{
serviceData: string;
get data():string{
return this.serviceData;
}
set data(value:string){
this.serviceData = value;
}
}
此服务显然已作为提供商添加到 app.module.ts..
在我的组件 A 上我有:
import { Component, OnInit } from '@angular/core';
import {dataBaseService} from "../dataBase.service";
@Component({
selector: 'app-tipo',
templateUrl: './tipo.component.html',
styleUrls: ['./tipo.component.scss'],
providers: [dataBaseService]
})
export class A implements OnInit {
constructor( public dataService:dataBaseService) {
this.dataService.serviceData = 'hello';
}
ngOnInit() {
console.log(this.dataService.serviceData);
}
}
直到这里一切都很好。如果我在控制台上显示:
console.log(this.dataService.serviceData);
它returns我"hello"如预期
但是在我的下一个组件上,当我再次打印相同的数据时,它显示未定义:
import { Component, OnInit } from '@angular/core';
import {dataBaseService} from "../dataBase.service";
@Component({
selector: 'app-modelo',
templateUrl: './modelo.component.html',
styleUrls: ['./modelo.component.scss'],
providers: [dataBaseService]
})
export class ModeloComponent implements OnInit {
ngOnInit() {
console.log(this.dataService.serviceData);
}
constructor(public dataService: dataBaseService) {
}
}
console.log(this.dataService.serviceData);
它returns"undefined"..
那么我如何保存我放在第一个组件上的数据并能够在另一个组件上显示它呢?我错过了什么?
更新:
像我这样的一些人在堆栈上找不到像这个问题这样的另一个问题的答案,那是因为您必须全局添加它们(在 module.ts 上),而不是单独添加提供者! !!!!!!!!!
正如您所说,您已经在模块级别添加了提供者,请尝试从组件中注释掉提供者声明。