使用JSON加载器在angular-cli中加载json文件,而无需编辑默认Webpack [英] Loading json file in angular-cli using json loader with out editing default webpack

查看:42
本文介绍了使用JSON加载器在angular-cli中加载json文件,而无需编辑默认Webpack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不编辑默认webpack-config.json文件的情况下使用json加载器在angular-cli中加载外部json文件?

Is it possible to Load external json file in angular-cli using json loader with out editing default webpack-config.json file ?

推荐答案

当然,您可以加载JSON,这就是为什么要使用json-loader的原因.在加载json的服务中,编写以下行:

Of course, you can load JSONs, that's why you have json-loader. In the service that loads the json write this line:

import * as someName from './somePath/someFile.json;

此行可能会引发错误: [ts]找不到模块'./somePath/someFile.json'.

This line may throw an error: [ts] cannot find module './somePath/someFile.json'.

那不是真正的错误(如果路径和文件名正确),而只有打字稿类型的错误,可以通过将该行添加到您的 typings.d.ts 文件中来轻松解决.(如果没有此文件,则可以在解决方案的 src 文件夹下创建该文件):

That not a real error (if the path and file name are correct), but only a typescript-type error that easily can be fixed by adding this line to your typings.d.ts file (if you don't have this file you can create it under the src folder in your solution):

/* allow import .json files */
declare module "*.json"
{
  const value: any;
  export default value;
}

这篇关于使用JSON加载器在angular-cli中加载json文件,而无需编辑默认Webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆