How to fetch data from a local JSON file in StencilJS?

情到浓时终转凉″ 提交于 2020-01-16 05:10:47

问题


I am learning stencilJS and wanted to fetch a JSON file from the local path. However, I am not able to do that until I have placed this file in www folder of the app. Problem is whenever I build my application file gets deleted/removed from the folder. I wanted to access this file from a services folder from my app. Kindly find below is my file structure and code.

src > services > mock.json

componentWillLoad() {
  return fetch("./services/mock.json")
    .then(response => response.json())
    .then(data => {
      this.mockData = data;
   });
}

config file

    import { Config } from '@stencil/core';
// import { sass } from '@stencil/sass';

export const config: Config = {
  namespace: 'mycomponent',
  globalStyle: 'src/globals/app.css',
  outputTargets:[
    { type: 'dist' },
    { type: 'docs' },
    {
      type: 'www',
      serviceWorker: null // disable service workers
    }
  ],
  plugins: [
    // sass()
  ]
};

Thank you in advance.


回答1:


try to add below step in your config file.

copy: [
    { src: 'services' }
  ]

it will copy the entire directory from src/services over to www/services



来源:https://stackoverflow.com/questions/54991630/how-to-fetch-data-from-a-local-json-file-in-stenciljs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!