0

Here is my code

@Component({
    selector: "workshops",
    template: template
})
export class Workshops {
    public userPhotoUrl: ko.Observable<string>;
    public resizing: ko.Computed<string>;
    public sections: ko.ObservableArray<ToolButton>;

    constructor(
        private readonly viewManager: ViewManager,
        private readonly userService: UserService,
                private readonly workshopSections: ToolButton[]
                
    ) {
        this.userPhotoUrl = ko.observable<string>(null);
                this.resizing = ko.pureComputed(() => this.viewManager.journeyName() ? "vertically horizontally" : "vertically horizontally suspended");
                console.log(this.workshopSections);
                console.log(this.workshopSections.length);
                // this.sections = ko.observableArray<ToolButton>(workshopSections);
                this.sections = ko.observableArray(this.workshopSections);
                // this.sections = this.workshopSections;
    }

    @OnMounted()
    public async loadUserProfile(): Promise<void> {
        const url = await this.userService.getUserPhotoUrl();
        this.userPhotoUrl(url);
    }

    public closeWorkshop(view: View): void {
        this.viewManager.closeWorkshop(view);
    }
}

Here is my output.

enter image description here

When I am reloading a page it gives me output as shown above.

but when I went to the page by router click it working correctly as below.

enter image description here

Arun verma
  • 31
  • 7
  • You wanted to ask why `this.sections` has length 0 or `this.workshopSections` has length 0 – Krzysztof Kaczyński Aug 05 '20 at 10:13
  • @KrzysztofKaczyński Yes, the same thing – Arun verma Aug 05 '20 at 10:48
  • The first log shows `[]` at the start and the second log shows `(7) [...]`. It means, the data is asynchronously loading in the first. When the `console.log` lines run, there was no data but it was populated in a later stage. It shows the array in the `console` because chrome console keeps a reference and shows the updated data, every time you expand it. If you tried `console.log(JSON.stringify(this.workshopSections))`, you'll see that it's an emtpy array when that line runs: [Console.log showing only the updated version of the object printed](https://stackoverflow.com/questions/17320181) – adiga Aug 09 '20 at 08:02
  • @adiga Actually the first image showing the output when I reload/refresh the page but when I come on the page by click on Link in reactjs then it works properly as shown in the second image. This is not an async issue. – Arun verma Aug 20 '20 at 03:39

0 Answers0