node.js - 在 Angular 2 应用程序中导入 Node js 模块

标签 node.js angular webpack electron

我想将 NodeJS 代理列表模块导入到 Angular2 应用程序中。 我正在将 Electron 与 Angular2 和 Webpack 配置结合使用。

  1. 这是我的 app.component.ts

    import { Component } from '@angular/core';
    const ProxyLists = require('proxy-lists');
    
    @Component({
      selector: 'app',
      templateUrl: './app/app.component.html',
    })
    
    export class AppComponent {
      proxies: any[] = [];
    
      constructor() {
      }
    
      getProxies() {
        let options = {
          countries: ['us', 'ca']
        };
    
    
    var gettingProxies = ProxyLists.getProxies(options);
    gettingProxies.on('data', function (proxies) {
      console.log(proxies);
    })
    
    } 测试() { console.log(“工作”); } }
  2. 这是 webpack.config.js

        var path = require('path');
        var webpack = require('webpack');
        var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;

    module.exports = { devtool: 'source-map', entry: { 'angular2': [ 'rxjs', 'reflect-metadata', 'zone.js', '@angular/core', '@angular/router', '@angular/http' ], 'app': './src/main' }, output: { path: __dirname + '/build/', publicPath: 'build/', filename: '[name].js', sourceMapFilename: '[name].js.map', chunkFilename: '[id].chunk.js' }, resolve: { extensions: ['.ts','.js','.json', '.css', '.html'], alias: { materializecss: 'materialize-css/dist/css/materialize.css', materialize: 'materialize-css/dist/js/materialize.js', } }, module: { loaders: [ { test: /\.ts$/, loader: 'ts-loader', exclude: [ /node_modules/ ] }, { test: /^((?!materialize).)*\.css$/, loader: 'raw-loader' }, { test: /.(png|woff(2)?|eot|ttf|svg)(\?[a-z0-9=\.]+)?$/, loader: 'url-loader?limit=100000' }, ] }, plugins: [ new CommonsChunkPlugin({ name: 'angular2', filename: 'angular2.js', minChunks: Infinity }), new CommonsChunkPlugin({ name: 'common', filename: 'common.js' }), new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", "window.jQuery": "jquery", Hammer: "hammerjs/hammer" }) ], target: 'electron-renderer' }; </pre>
  3. 当我运行该应用程序时,出现此错误。 app.png

最佳答案

我从未使用过 angularjs2 ,但你应该在你的 Nodejs 服务器脚本上将这些模块静态化。

app.use('/angular2', express.static(path.join(__dirname, '/node_modules')))

关于node.js - 在 Angular 2 应用程序中导入 Node js 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42688279/

相关文章:

angular - CdkVirtualScrollViewport 已附加(错误)

angular - 找不到模块 : Error: Can't resolve 'classlist.js' in angular cli 6. 1.3

node.js - 如何将nextjs部署到非根目录的目录中

angular - 覆盖 Angular 中的浏览器 `back` 按钮功能?

javascript - app.js 中需要错误

用作关键帧步长百分比的变量上的 WebPack SCSS 错误 "uknown word"

javascript - 使用 npm 在 html 中导入 javascript

node.js - 使用 `npm i`时未生成package-lock.json(npm v6.3.0,node v8.9.2)

node.js - 在 Node 中返回 JSON 数组

node.js - koa中独立的路由器和 Controller