1

I have task (auth): All data should be stored in LocalStorage. Application should be written without back-end languages or services You should create a json file with 10 test users and get it content while application forming. Save user to LocalStorage.

I create a json file with data of users, but i don't understand how to fetch it in my service.

{
  "Users": [
    {
      "Id": 1,
      "name": "FirstUser",
      "email": "[email protected]",
      "password": "test123"
    },
    {
      "Id": 2,
      "name": "2thUser",
      "email": "[email protected]",
      "password": "test123"
    }
  ]
}

I tried some tricks like

"import as data from '../shared/users.json'
"const users = data.Users"
(error on Users)

Do you have idea "how to" ?

3

1 Answer 1

1

I think it should be import * as data from '../shared/users.json but your system.config might add .ts or .js extension at the end of it.

The way I prefer to do it is to export it as a variable inside a ts file

export var data = {
  "Users": [
    {
      "Id": 1,
      "name": "FirstUser",
      "email": "[email protected]",
      "password": "test123"
    },
    {
      "Id": 2,
      "name": "2thUser",
      "email": "[email protected]",
      "password": "test123"
    }
  ]
}

then import that variable inside your component

import { data } from '../shared/users';

Full plunker example: http://plnkr.co/edit/CGtxYJkcjYt2cEzrbL00?p=info

Sign up to request clarification or add additional context in comments.

6 Comments

Of course, ts file better and more comfortable, but it's my technical task with "json data" ... about "import * as data from ...." i did that ... and add to system.configjs extension ...
i forget, i'll test now. About this trick, i did all like there hackernoon.com/…
@МаксимТарсис but you are missing the * be careful about that
pp.userapi.com/c636321/v636321071/56991/Sie4ttIA_dE.jpg (error: Property "Users" does not exist on type 'typeof"*.json"' )
@МаксимТарсис without http I don't think that'll be possible tbh :/
|

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.