Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to retain a service's data between components in Angular 8 ?

Parent

How to retain a service's data between components in Angular 8 ?

+3
−0

I would like to maintain a log that is available to several modules in my Angular 8 system. So I created the following service:

@Injectable({
	providedIn: 'root'
})
export class LogService {
	_log: string[] = [];
	
	constructor() {	}
	
	log(labelMsg: string, msg?: string | object) {
		if (msg && typeof msg != 'string')
			msg = JSON.stringify(msg);
		
		msg = msg ? `${labelMsg}: ${msg}` : labelMsg;
		this._log.push(`[${moment().format("DD/MM/YYYY HH:mm:ss.SSS")}] - ${msg}`);
	}
	
	show() {
		if (this._log.length)
			this._log.forEach(l=>console.log(l));
		else 
			console.log('Empty log');
		
	}
}

But the problem is that when I load I inject it on several components, I get a per-component instance. I mean I log things in app-init, MainComponent and AboutComponent. And each time I change components, .show will output "Empty log". I don't want to persist this data. I want it to live only in the frontend.

How can I retain the data between components ?

Edit

Code of component where I used LogService:

(...)
import { LogService } from 'src/app/shared/services/log.service';
(...)

@Component({
	selector: 'app-show-account',
	templateUrl: './show-account.component.html',
	styleUrls: ['./show-account.component.scss']
})
export class ShowAccountComponent implements OnInit {
        (...)
	constructor(
                (... other services)
		private logService: LogService
	) { }

	ngOnInit() {
                (...)
		this.logService.log('Show account');
	        (...)
        }
        (...)
}

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (2 comments)
Post
+3
−0

Found the problem: I wasn't using router in some links and the whole app was restarted.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
General comments
Alexei‭ wrote over 3 years ago

Now everything makes sense. Btw - page refreshes are typically noticeable (some flicker). Is this happening in your case? Thanks for posting an answer.

nelson777‭ wrote over 3 years ago

Yes it was. My mistake.