1

I am trying to access mock JSON data from data.mock.ts file into ‘data.stories.ts’ file. I tried writing some code in getWindowsData function which is in stories file but I am unable to access the data

Here is my code for data.mock.ts file

export class Windows {
  public static readonly Windows = {
    Windows: [
      {
        WindowsValue: 993,
        WindowsYear: 1997,
      },
      {
        WindowsValue: 953,
        WindowsYear: 1873,
      },
    ],
  };
}

Here is my code for data.stories.ts file


export function HttpLoaderFactory(https: https) {
  return new TranslateHttpLoader(https, './i18n/', '.json');
}

@NgModule()
export class WindowsStoriesComponent {
  constructor(private translate: TranslateService, private localeService: LocaleService) {
    localeService.nameObservable().subscribe((name) => {
      translate.use(name);
    });
  }
}

      },
    };
  });

How can I get the data from mock file to stories file?

1 Answer 1

1

What you are doing is accessing the class as whole, Instead you can write in the function you mentiond, like this,

export function getEmpiricalPercentilesData(params: HttpParams) {

  // Returning the *empiricalPercentiles* variable(Mock Data) of the class
  return EmpiricalPercentiles.empiricalPercentiles;
}
Sign up to request clarification or add additional context in comments.

4 Comments

It is giving following error- TS2576: Property 'empiricalPercentiles' is a static member of type 'EmpiricalPercentiles'
Oh sorry mate, My bad, static member can be accessed directly without creating the instance of the class, I have edited my answer, Please check, and if it works please mark it as correct.
Yes it worked to certain extent, data is being displayed in Empirical Percentiles column but 2nd column it says N/A, no data is displayed. Any idea?
Can you create the stackblitz rnvironment for your code, SO I can understand it better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.