【问题标题】:Import node js module in angular 2 application在 Angular 2 应用程序中导入节点 js 模块
【发布时间】:2017-07-30 00:30:31
【问题描述】:

我想将 nodejs 代理列表模块导入 angular2 应用程序。 我正在使用带有 Angular2 和 Webpack 配置的 Electron。

  1. 这是我的 app.component.ts

    从'@angular/core'导入{组件}; const ProxyLists = require('proxy-lists'); @零件({ 选择器:'应用程序', templateUrl: './app/app.component.html', }) 导出类 AppComponent { 代理:任何[] = []; 构造函数(){ } getProxies() { 让选项 = { 国家:['我们','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

【问题讨论】:

  • 你可以通过在 types 中声明它然后正常导入来导入它,所以 import * as yourname from 'proxy-list';如果一种类型不存在,您可以通过将其添加到您的 typings.d.ts 文件(在 src 中)然后声明它来创建一个 'declare var proxyLists: any;

标签: node.js angular webpack electron


【解决方案1】:

我从未使用过 angularjs2 ,但您应该在您的 nodejs 服务器脚本上将这些模块设为静态。

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

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 2017-05-19
    • 2017-05-02
    • 1970-01-01
    • 2018-08-02
    • 2017-04-01
    • 2016-11-29
    • 1970-01-01
    • 2016-12-25
    相关资源
    最近更新 更多