Angular 2在初始页面加载时请求太多

时间:2016-11-10 06:57:35

标签: javascript angular rxjs systemjs

我是Angular 2的新手,我知道我的查询肯定会有一些解决方案但是在发布这个问题的时候我运气不好来解决这个问题。 基本上我已经按照Angular.io上给出的指令创建了一个简单的Angular 2应用程序,并且我已经添加了一些更多的组件。但是在运行我的Angular 2应用程序后,我注意到它已经提出了100多个请求来获取我所创建的每个组件和角度库。我确实尝试将所有角度库捆绑到一个文件并使用SystemJS动态文件加载器导入该文件但在此之后我已经获得了角度库的404(文件未找到错误)。我已经看过以下解决方案,例如How can you bundle Angular 2 using System JS Builder?How to bundle Angular2 RC1 with systemjs,但没有成功。我希望最小化我的应用程序在初始加载时所做的请求,以便我的应用程序快速加载(现在它发送了100多个请求,大约需要20秒来加载应用程序)

的index.html

<html>
  <head>
    <base href="/">
    <title>My Angular 2</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="public/assets/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="public/css/bundle-css.min.css">
  </head>
  <!-- 3. Display the application -->
  <body>

    <my-app>
      Loading...
    </my-app>

    <!-- 1. Load libraries -->
     <!-- Polyfill(s) for older browsers -->
    <script src="public/js/bundle-main.min.js"></script>

    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>

    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>

    <script src="public/js/bundle-js.min.js"></script>
  </body>
</html>

SystemJS.config.js

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    defaultJSExtensions: true,
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'public',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',

      // Vendor libraries
      'ng2-tooltip': 'npm:ng2-tooltip',
      'angular2-modal': 'npm:angular2-modal',
      'angular2-modal/plugins/bootstrap': 'npm:angular2-modal/bundles'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'angular-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      },

      //Vendor libraries
      'ng2-tooltip': { 
        main: 'index.js', 
        defaultExtension: 'js' 
      },
      'angular2-modal': { 
        defaultExtension: 'js', 
        main: 'bundles/angular2-modal.umd' 
      },
      'angular2-modal/plugins/bootstrap': { 
        defaultExtension: 'js', 
        main: 'angular2-modal.bootstrap.umd' 
      }
    }
  });
})(this);

目录结构

|_ app
    |_ assets
    |_ components
       |_ MyComponents
          |_ MyComponents.component.ts
          |_ MyComponents.component.html
          |_ MyComponents.routes.ts
          |_ MyComponents.service.ts
          |_ index.ts
    |_ scss
    |_ js
    |_ main.ts
    |_ app.component.ts
    |_ app.component.html
    |_ app.module.ts
    |_ app.routes.ts
 |_ node_modules
 |_ typings
 |_ gulpfile.js
 |_ index.html
 |_ package.json
 |_ systemjs.config.js
 |_ tsconfig.json
 |_ tsd.json
 |_ typings.json

0 个答案:

没有答案