2017-02-04 2 views
0

저는 Angular 2 + Angular CLI를 사용하여 응용 프로그램을 작성하고 있습니다. 데모를 기반으로 원했던 바로이 멋진 라이브러리 'csvtojson'을 찾았습니다. 그래서 프로젝트에 포함하려고합니다. 그러나, 내 첫 번째 질문은 그것이 내 프로젝트에이 라이브러리를 포함시킬 수 있습니까? Angular 2를 위해 빌드 된 것을 보지 못했습니까? 만약 그렇다면 ... 나는 단지 눈을 멀게보고 슬프게 느낄 수 있다는 것을 의미합니까?Angular 2 용 외부 라이브러리 사용

예인 경우 ... 어떻게 구현합니까? 이 libs와 노드 서버에서 실행해야하기 때문에 내가

1. npm i --save csvtojson 
2. inside app.module.ts 
    import { csvtojson } from 'csvtojson' 
    providers: [ 
     {{...}} 
     csvtojson //test 
    ], 

3. app.component.ts 
    import { csvtojson } from 'csvtojson' 
    constructor(private csvToJson: csvtojson) { } 
    this.csvToJson.csv({ noheader: true }) 
     .fromString(data) 
     .on('csv', (csvRow) => { // this func will be called 3 times 
      console.log('afasf',csvRow) // => [1,2,3] , [4,5,6] , [7,8,9] 
     }) 
     .on('done',() => { 
      //parsing finished 
     }) 

작동하지 않습니다 아래했을 그리고이 오류를

ERROR in ./~/csvtojson/libs/core/workerMgr.js Module not found: Error: Can't resolve 'child_process' in '/Users/james/Desktop/web-app/nod e_modules/csvtojson/libs/core'

답변

0

을받을 것은, 당신은 브라우저에서 child_process을 가질 수 없습니다.

브라우저를 들어,이 솔루션은 당신에게 https://www.bennadel.com/blog/1504-ask-ben-parsing-csv-strings-with-javascript-exec-regular-expression-command.htm

+0

에 대한 Plunker 작업 여기

? 브라우저 용 – user172902

+0

이 솔루션을 사용할 수 있습니다. https://www.bennadel.com/blog/1504-ask-ben-parsing-csv-strings-with-javascript-exec-regular-expression-command.htm –

0

외부 라이브러리 필요 없음을 도울 수 있습니다. 즉,이 라이브러리 내 손이 닿지 않는 곳에 단순히 의미 하는가 csvTOjson

import {Component, NgModule} from '@angular/core' 
    import {BrowserModule} from '@angular/platform-browser' 

    import { Component } from '@angular/core'; 

    @Component({ 
     selector: 'my-app', 
     templateUrl: './app.html' 
    }) 
    export class AppComponent { 
     title = 'csvTOjson works!'; 
     text : any ; 
     JSONData : any; 
     csvJSON(csvText) { 
     var lines = csvText.split("\n"); 

     var result = []; 

     var headers = lines[0].split(","); 
     console.log(headers); 
     for (var i = 1; i < lines.length-1; i++) { 

      var obj = {}; 
      var currentline = lines[i].split(","); 

      for (var j = 0; j < headers.length; j++) { 
       obj[headers[j]] = currentline[j]; 
      } 

      result.push(obj); 

     } 

     //return result; //JavaScript object 
     console.log(JSON.stringify(result)); //JSON 
     this.JSONData = JSON.stringify(result); 
    } 

    convertFile(input) { 

    const reader = new FileReader(); 
    reader.readAsText(input.files[0]); 
    reader.onload =() => { 
     let text = reader.result; 
     this.text = text; 
     console.log(text); 
     this.csvJSON(text); 
    }; 

    } 
    } 


    @NgModule({ 
     imports: [ BrowserModule ], 
     declarations: [ AppComponent ], 
     bootstrap: [ AppComponent ] 
    }) 
    export class AppModule {} 
관련 문제