2016-12-15 53 views
2

我使用节点模块例如“LWIP”在如何反应的组成部分?这是用于电子应用的。调用节点模块从反应成分

更新与代码的问题:

  1. 这是从我试图调用另一个.js文件的反应成分。

button.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import resize from '../../node-code/process'; 

class Button extends React.Component{ 

    mess(){ 
     console.log('working'); 
     resize(); 
    } 

    render(){ 
     return <button id="imgButton" onClick={this.mess.bind(this)}>Upload Image</button> 
    } 
} 

export default Button 
  • 这是其他JavaScript文件,其中我试图调整图像大小。
  • process.js

    var lwip = require('lwip'); 
    
    export default function(){ 
        var lwip = require('lwip'); 
        lwip.open('../../public/img/portrait.jpg', function(err, image){ 
    
    
        image.batch() 
         .scale(0.75)   // scale to 75% 
         .rotate(45, 'white') // rotate 45degs clockwise (white fill) 
         .crop(200, 200)  // crop a 200X200 square from center 
         .blur(5)    // Gaussian blur with SD=5 
         .writeFile('../../public/img/output.jpg', function(err){ 
    
         }); 
    
        }); 
    } 
    
    +0

    欢迎使用堆栈!这个问题需要一些工作。你遇到任何错误? – azium

    +0

    没有错误,但我需要知道调用节点模块中的方法的过程。我正在写一个电子应用程序,我需要处理一个图像,所以我安装了lwip节点模块,如何在反应组件中使用这些方法?有没有可以指导我完成的教程? – Vasista

    +0

    like ...'var lwip = require('lwip'); lwip.method()'? – azium

    回答

    1

    节点模块需要从主电子线程,而不是渲染线程上运行作出反应运行。

    您可以在渲染器进程中运行NPM模块,就好像您在浏览器中一样,但这些模块无法使用Node.js库,因为浏览器中显然没有节点。

    要你需要使用IPC(进程间通信),使用事件在线程之间发送数据的系统的主(节点)和渲染器(浏览器)线程之间进行通信。

    Here's the IPC documentation for Electron.

    如果需要线程之间不断的沟通,你可以使用electron-ipc-socket库。