2017-07-17 54 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