I made a modification to the default .NET Core SPA template by adding a menu item in the nav-menu.component.html
file like this:
<li [routerLinkActive]='["link-active"]'>
<a [routerLink]='["/fetch-data"]' (click)='collapse()'>
<span class='glyphicon glyphicon-th-list'></span> Requests ({{count}})
</a>
</li>
This displays the count of requests fetched from the server inside the parentheses. I initialized the count in my TypeScript file as follows:
export class NavMenuComponent {
count: number;
constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) {
var url = this.baseUrl + "api/request/TheCount/";
this.http.get<number>(url).subscribe(result => {
this.count = result;
}, error => console.error(error));
}
The TheCount
method simply retrieves the count of requests from the server like this:
[HttpGet("TheCount")]
public async Task<IActionResult> TheCount()
{
var count = await GetRequestsCount();
return new JsonResult(count, new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
}
Everything is working correctly and the count is displayed as expected inside the parenthesis. However, the issue arises when a request is deleted from another component, the count variable in the nav-menu.component
isn't updated automatically. This means that I have to refresh the site in order to see the updated count again. Is there a way to automatically update the count after changes?