Local JSON file in Vue

How to read local JSON file in Vue

Sat Jun 19 2021

Sometimes we need to read data from a local file, such as Mock Data which can be stored in the form of JSON format.

Create folder for mock data and place the JSON inside the folder.

\[
   { "id": 1, "title": "work out from json", "isdone": true },
   { "id": 2, "title": "Fallback", "isdone": false }

\] 

and in the component we can read the file using an import statement.

import  taskData from '@/mockData/taskData.json'

we can place the data as a state in store as follows

state: {       
    tasks: taskData, 
  },

Comments