【问题标题】:How do I structure the import from a third-party angular2 app如何构建从第三方 angular2 应用程序的导入
【发布时间】:2016-07-08 18:22:08
【问题描述】:

我不确定如何为第三方库构建导入语句。

我正在尝试使用 Visual Studio 中 angular2 项目中的 ng2-file-upload。 https://github.com/valor-software/ng2-file-upload

我的组件中有这个:

从 'ng2-file-upload' 导入 {FILE_UPLOAD_DIRECTIVES, FileUploader};

使用 Visual Studio,这将构建并智能感知在库中查找指令。在运行时我收到此错误:

GET http://localhost:54675/ng2-file-upload404(未找到)

显然,这不是正确的路径。

下面是我的目录结构。导入在 documentUpload.component.ts 中。

> wwwroot
> -------app
> ----------shared
> -------------documentUpload
> ----------------documentUpload.component.ts
> -------node_modules
> ----------ng2-file-upload
> -------------ng2-file-upload.js
> -------------components
> ----------------file-upload
> -------------------file-uploader.js

我的 ng2-file-upload 源位于项目根目录中,一个 gulp 文件仅将 js 和 css 移动到我的 wwwroot/node_modules 文件夹中。我认为在设计时它在我的根目录中使用源代码(因此 intellisense 工作并构建)但在运行时它试图从 wwwroot 中提取并且缺少某些内容或引用错误。

更新: 根据反馈,我将此模块添加到 system.config 中,如下所示:

    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        },
        map: {
            'ng2fileupload': 'node_modules/ng2-file-upload/ng2-file-upload.js'
        }
    });

  System.import('app/boot')
        .then(null, console.error.bind(console));
  System.import('ng2fileupload')
        .then(null, console.error.bind(console));

此时,我在加载时收到 404 错误: http://localhost:54675/node_modules/ng2-file-upload/components/file-upload/file-select

该文件选择组件位于 ng2-file-upload 下的子目录中。我将它添加为地图模块,但它只会为它使用的其他组件产生类似的错误。

当前进度 这是我的 index.html 文件。

<!DOCTYPE html>
<html>

<head>
    <title>FMS</title>
    <base href="/" />
    <!-- 1. Load libraries -->
    <script src="/node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="/node_modules/systemjs/dist/system.src.js"></script>
    <script src="/node_modules/rxjs/bundles/Rx.js"></script>
    <script src="/node_modules/angular2/bundles/angular2.dev.js"></script>
    <script src="node_modules/angular2/bundles/router.dev.js"></script>
    <script src="node_modules/es6-shim/es6-shim.js"></script>

    <script src="/node_modules/jquery/dist/jquery.js"></script>
    <script src="/node_modules/bootstrap/dist/js/bootstrap.js"></script>
    <script src="/node_modules/angular2/bundles/http.js"></script>
    <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css" />
    <link rel="stylesheet" href="/node_modules/spinkit/css/spinkit.css" />

    <link rel="stylesheet" href="/app/assets/site.css" />

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        },
        map:{
        'ng2-file-upload':'node_modules/ng2-file-upload/ng2-file-upload.js'
        }

      });
      System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>

</head>

<!-- 3. Display the application -->
<body>
    <fmssupport-app></fmssupport-app>
</body>

</html>

这是我的组件:

import {Component} from 'angular2/core';
import {FILE_UPLOAD_DIRECTIVES} from 'ng2-file-upload';

@Component({
    selector: 'document-upload',
    templateUrl: 'app/shared/documentUpload/documentUpload.component.html',
    directives: [FILE_UPLOAD_DIRECTIVES]
})

export class DocumentUploadComponent {

}

这些是我得到的错误:

获取 http://localhost:54675/node_modules/ng2-file-upload/components/file-upload/file-select 404 错误:加载 XHR 错误(404 未找到) http://localhost:54675/node_modules/ng2-file-upload/components/file-upload/file-select(…) 得到 http://localhost:54675/node_modules/ng2-file-upload/components/file-upload/file-drop 404 获取 http://localhost:54675/node_modules/ng2-file-upload/components/file-upload/file-uploader 404(未找到)

从这里我可以看到它正在寻找 ng2-file-upload 组件,但是当该组件尝试在子文件夹中加载组件时它失败了。

【问题讨论】:

    标签: angular angular2-directives


    【解决方案1】:

    通常您通过 index.html 中的 System.config 文件执行此操作,该文件调用您的 Angular 应用程序。

    类似这样的:

    System.config({
            defaultJSExtensions: true,
            packages: {
                "/angular2": {"defaultExtension": false}
            },
            map: {
                'ng2-file-upload': 'node_modules/ng2-file-upload'
            }
    });
    

    您会注意到map: 函数,这是在 angular2 中配置其他包的简单方法。

    编辑:

    您的 index.html 文件应如下所示:

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system.js"></script>
    <script src="node_modules/ng2-file-upload></script> //Note how I am grabbing it here.
    <script>
         System.config({
            packages: {
              app: {
                format: 'register',
                defaultJSExtensions: true
              },
            },
            map: {
                "ng2-file-upload": "/node_modules/ng2-file-upload"//This is so we map it to the name ng2-file-upload for use in our application.
              }
          });
    </script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.js"></script>
    <script src="node_modules/angular2/bundles/http.min.js"></script>
    <script src="node_modules/angular2/bundles/router.min.js"></script>
    <script>
      System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>
    

    完成此操作后,您应该能够:

    import {Ng2-file-uploader} from 'ng2-file-upload'
    

    我不确定 ng2-file-upload 的导出是什么,所以你必须找到名称,如果文件的文件夹是 3 个目录,你必须像这样引用它:

    'ng2-file-upload/second_directory/third_directory/name_of_component'
    

    这应该让它启动并运行。

    System.js 上的文档非常好并且易​​于阅读,这是了解如何从其 github 向其中添加插件的好方法。 https://github.com/systemjs/systemjs/blob/master/docs/overview.md#plugin-loaders

    希望这会有所帮助!抱歉昨天没发,这里大风雪停电一整天!

    编辑 2:

    好的,有几件事,您在map 函数中指定了一个文件而不是一个目录,如果您只想使用该特定文件进行ng2-file-upload,这没关系。但是您实际上仍然拼错了文件,这让您有些头疼,应该是ng2-file-uploader.js。

    还有,

    {FILE_UPLOAD_DIRECTIVES} from 'ng2-file-upload';
    

    该导入语句无效,ng2-file-upload 中没有FILE_UPLOAD_DIRECTIVES。它只有一个FileUploader Class。您可以从他们的来源https://github.com/valor-software/ng2-file-upload/blob/master/components/file-upload/file-uploader.ts 中检查自己。

    你可以使用:

    import {FileUploader} from 'ng2-file-upload'
    

    import {FileUploader as FILE_UPLOAD_DIRECTIVES} from 'ng2-file-upload'
    

    我认为您遇到的一些问题是您没有指定目录而仅直接指定文件。这就是您遇到错误的原因:

    http://localhost:54675/node_modules/ng2-file-upload/components/file-upload/file-select

    因为您没有引用从目录中拉取文件选择模块,所以您只会得到文件上传器。您可以解决此问题,但在您的 HTML 文件中您根本没有引用此脚本。你需要有一个脚本标签:

    <script src="node_modules/ng2-file-upload></script>
    

    就像我之前提到的那样。

    【讨论】:

    • 同样的错误信息?你能说明你是如何在你的组件中导入它的吗?
    • 我用 system.config 更新了帖子。如何查看加载了哪些模块?
    • 嗯,我的意思是你是如何在你的组件中访问它的?您使用的是什么导入语句?另外我假设您的 app.js 位于 wwwroot/app 而不是 wwroot/app/shared 对吗?
    • 我更新了帖子并添加了导入和更多错误消息。你是正确的应用程序,js 在哪里。
    • 你能发布你的整个 index.html 文件吗?并发布您正在使用它的组件?你也不应该做 2 system.import。我认为这样做会创建 2 个单独的“系统”,尽管我对此不确定 100%。我会编辑我的帖子来帮助你。
    【解决方案2】:

    Index.html.. 其中“libs”是您在 wwwroot 下的 .js 文件夹

     System.config({
            packages: {
                appScripts: {
                    defaultExtension: 'js'
                }
                , 'ng2-file-upload': { defaultExtension: 'js' }
                    }, map: { 'ng2-file-upload': 'libs/ng2-file-upload/' }
                }
    
        );
    

    和组件... {Directpry/File}

    import {FILE_UPLOAD_DIRECTIVES, FileUploader} from 'ng2-file-upload/ng2-file-upload';
    

    【讨论】:

      【解决方案3】:

      基本上我正在关注@Morgan G 的回答,但我有点搞砸了。现在我在这里提供我的代码,希望对其他人有帮助。


      1. 安装 ng2-file-upload npm install ng2-file-upload --save
      2. 在 index.html 中导入
      <script src="node_modules/es6-shim/es6-shim.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
      <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
      <script src="node_modules/systemjs/dist/system.js"></script>
      <script src="node_modules/ng2-toastr/bundles/ng2-toastr.min.js"></script>
      
      <script src="node_modules/rxjs/bundles/Rx.js"></script>
      <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
      <script src="node_modules/angular2/bundles/router.dev.js"></script>
      <script src="node_modules/angular2/bundles/http.dev.js"></script>
      
      <!-- 2. Configure SystemJS -->
      <script>
        System.config({
          packages: {
            app: {
              format: 'register',
              defaultExtension: 'js'
          },
          'ng2-file-upload': { "defaultExtension": "js" }
          },
        map: {
             "ng2-file-upload": "/node_modules/ng2-file-upload"
           }
        });
        System.import('app/main')
              .then(null, console.error.bind(console));
      </script>
      

      3。导入指令:

      @Component({
      ...
      directives: [FILE_UPLOAD_DIRECTIVES] })
      

      还有import {FILE_UPLOAD_DIRECTIVES, FileUploader} from 'ng2-file-upload/ng2-file-upload'; 4.然后你应该可以添加

      <input type="file" ng2-file-select [uploader]="uploader" multiple  />
      

      在您的模板中

      【讨论】:

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