'的任务是什么;cliSystemConfigPackages';对象在angular 2 CLI配置中

'的任务是什么;cliSystemConfigPackages';对象在angular 2 CLI配置中,angular,systemjs,angular2-cli,Angular,Systemjs,Angular2 Cli,我正在使用angular 2和CLI 因为没有关于这方面的信息 关于桶与angular 2 CLI工具组合的含义,我在这里询问 到目前为止,我的angular 2应用程序工作正常,这意味着我在运行时没有收到任何错误。但我担心这种情况迟早会改变,因为我可能无法正确使用angular 2 CLI。这意味着我并不总是使用ng generate component my new component来创建组件。当我使用它时,我会删除每个组件的createindex.ts,因为我认为我不需要它 我这里有三个

我正在使用angular 2和CLI

因为没有关于这方面的信息

关于桶与angular 2 CLI工具组合的含义,我在这里询问

到目前为止,我的angular 2应用程序工作正常,这意味着我在运行时没有收到任何错误。但我担心这种情况迟早会改变,因为我可能无法正确使用angular 2 CLI。这意味着我并不总是使用
ng generate component my new component
来创建组件。当我使用它时,我会删除每个组件的createindex.ts,因为我认为我不需要它

我这里有三个问题:

  • 什么是桶?档案?文件夹?一批组件
  • 什么意思:这里有硬编码“索引”的主属性?cliSystemConfigPackages[barrelName]={main:'index'}
  • 当以下配置由CLI管理时,如果我不使用CLI,会发生什么情况
  • 这是system.config.ts的一部分

    /***********************************************************************************************
         * Everything underneath this line is managed by the CLI.
         **********************************************************************************************/
        const barrels: string[] = [
          // Angular specific barrels.
          '@angular/core',
          '@angular/common',
          '@angular/compiler',
          '@angular/http',
          '@angular/router',
          '@angular/platform-browser',
          '@angular/platform-browser-dynamic',
    
          // Thirdparty barrels.
          'rxjs',
    
          // App specific barrels.
          'app',
          'app/shared',
          'app/test/create',
          'app/test/edit',
          'app/administration' 
          /** @cli-barrel */
        ];
    
        const cliSystemConfigPackages: any = {};
        barrels.forEach((barrelName: string) => {
          cliSystemConfigPackages[barrelName] = { main: 'index' };
        });
    
  • 什么是桶?档案?文件夹?一批组件
  • 定义取自:

    考虑创建一个导入、聚合和重新导出项目的文件。我们称此技术为桶

    假设您有一个空文件夹
    src/app/components
    ,在该文件夹中运行
    ng generate component MyComponent
    。这将创建一个包含5个文件的文件夹:

    |-- my-component
    |   |-- my-component.component.css
    |   |-- my-component.component.html
    |   |-- my-component.component.spec.ts
    |   |-- my-component.component.ts
    |   |-- index.ts
    
    index.ts
    是桶,其目标是重新导出
    my component.component.ts
    中的类,因此其内容将是:

    export * from './my-component.component';
    
    为了使示例更加清晰,现在我们生成一个组件
    MyComponent2
    ,它将拥有自己的文件夹和
    index.ts

    现在,我们在另一个类中使用这两个组件,通过三种不同的方式导入它们:

    1.我们根本不使用桶:

    import { MyComponent } from './components/my-component/my-component.component';
    import { MyComponent2 } from './components/my-component2/my-component2.component';
    
    2.我们使用桶,但不在system.config.ts中添加桶:

    import { MyComponent } from './components/my-component/index';
    import { MyComponent2 } from './components/my-component2/index';
    
    import { MyComponent } from './components/my-component';
    import { MyComponent2 } from './components/my-component2';
    
    3.我们使用桶,并将其添加到system.config.ts中:

    import { MyComponent } from './components/my-component/index';
    import { MyComponent2 } from './components/my-component2/index';
    
    import { MyComponent } from './components/my-component';
    import { MyComponent2 } from './components/my-component2';
    
    在最后一种情况下,我们甚至不必指明索引文件,只需将路径写入保存该文件的文件夹即可

    基本上,桶的进口报表越来越短。在这种情况下,我们只是每桶出口一类,但您可以在一桶中出口任意数量的产品

    除此之外,我们还可以再出口桶。例如,我们可以在
    components
    文件夹中有一个
    index.ts
    ,其中包含以下两个导出:

    export * from './my-component';
    export * from './my-component2';
    
    现在我们可以这样导入它们:

    import { MyComponent, MyComponent2 } from './components';
    
    或者只是:

    import * from './components';
    
    2.什么意思:此处带有硬编码“索引”的主要属性?cliSystemConfigPackages[barrelName]={main:'index'}

    正如您可能已经猜到的,这说明我们的桶将是名为
    index
    的文件。因此,对于我们在
    barkes
    数组中指定的每个文件夹,其中都必须有一个
    index.ts
    文件(编译时是一个
    index.js

    数组名为
    barries
    可能有点让人困惑,但它实际上包含了包含barrier文件的文件夹,这可能就是为什么您在问题1中询问barrier是文件、文件夹还是一批组件的原因。在他们的样式指南中,他们将其定义为文件

    3.当以下配置由CLI管理时,如果我不使用CLI,会发生什么情况

    这将取决于我们如何导入文件,但如果我像这样导入
    MyComponent
    ,则会出现一个常见错误:

    import { MyComponent } from './components';
    
    我已经准备好了所有的桶,但是我忘了在SystemJS
    system config.ts
    文件中的桶数组中添加
    app/components/my component
    (使用ng generate时会自动添加它们),应用程序编译时不会出现错误,但每当需要加载
    MyComponent
    时就会失败,我会在控制台中看到此错误:

    Failed to load resource: the server responded with a status of 404 (Not Found) 
    http://localhost:4200/app/components/my-component.js 
    
    这是因为在
    app/components/index.ts
    中,我将
    export*from./my component'
    放在了
    my component
    中,因为SystemJS不知道这是一个桶,因此没有告诉模块系统应该在
    my component
    文件夹中搜索名为
    index.js
    的文件,它只是去尝试获取一个名为
    my component.js
    的文件


    现在我只想坚持他们的最佳实践,在每个文件夹中都有一个
    index.ts
    文件,并让它导出其中的所有内容,包括子文件夹中的其他桶,并将所有路径添加到包含
    system config.ts
    中的
    index.ts
    的文件夹中

    我很惊讶如此出色的答案竟然没有打勾。