2017-07-17 150 views
0

我做了一个新的安装角4项目,我试图加载故宫包steem在这样的:不能导入角4 NPM包,需要一个JSON文件

app.component .TS

import {Component, OnInit} from '@angular/core'; 
import * as steem from 'steem'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent implements OnInit { 
    title = 'app works!'; 

    ngOnInit() { 
    steem.api.getAccounts(['test', 'test2'], function(err, response){ 
     console.log(err, response); 
    }); 
    } 
} 

然而,出现以下错误:

Failed to compile. 

./~/steem/lib/api/index.js 
Module not found: Error: Can't resolve './methods' in '/Users/Stephan/Dev/kanzeon/node_modules/steem/lib/api' 
@ ./~/steem/lib/api/index.js 17:15-35 
@ ./~/steem/lib/index.js 
@ ./src/app/app.component.ts 
@ ./src/app/app.module.ts 
@ ./src/main.ts 
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts 

在我看来,这是造成的,因为在./methods,它解决不了是一个JSON文件。 Angular-cli的webpack配置包含一个json加载器(并且我已经安装了json-loader npm包),但在加载器中存在线路exclude: 'node_modules'。我真的不想重写我自己的angular-cli的webpack配置(有一个很好的理由,为什么他们隐藏它的实现,它的配置非常有效),但这确实对我的应用程序造成了问题。

有没有办法确保我可以包含steem npm包并让它加载其json文件而不覆盖webpack配置?

+0

...并由steemit团队解决,他们只是用js文件替换了json https://github.com/steemit/steem-js/issues/210#issuecomment-315809234 –

回答

2

STEEM-JS需要JSON文件内部,所以你需要JSON装载机配置

https://github.com/steemit/steem-js/tree/master/examples/webpack-example

所以我恐怕你必须扩展/覆盖角CLI的配置的WebPack。不幸的是,它似乎有角度doesn't yet provide a way to extend/override their webpack config

hooks有可能吗?

+0

谢谢。从文档中我不清楚钩子如何解决我的问题。我应该创建一个单独的npm模块,它有效地提供一个钩子来在构建时注入'steem'模块吗? –

+0

我有0经验角-cli:')钩看起来很有希望为您的问题,但我想他们不是... – Flauwekeul