简体   繁体   English

条件负载Angular2

[英]Conditional load Angular2

I need make redirect to login page, before load Angular2 application(without loading it). 我需要重定向到登录页面,然后再加载Angular2应用程序(不加载它)。 My project is based on angular2-quicksart 我的项目基于angular2-quicksart

When I load angular2 js file after uglifyjs 当我在uglifyjs之后加载angular2 js文件时

<script>
    var cookie = $.cookie(COOKIE_NAME);
    if (cookie == undefined){
        window.location.href = LOGIN_SERVER_URL
    } else {
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.src = "build/app/bundle.min.js";
        head.appendChild(script);
        head.innerHTML += ("<script>System.import('app').catch(function(err){ console.error(err); }); <\/script>");
    }
</script>

This solution works. 此解决方案有效。 JS was loaded and application is running. JS已加载,应用程序正在运行。

When I use systemjs file to load Angular app, systemjs.config.js was loaded, but app doesn't run. 当我使用systemjs文件加载Angular应用程序时,已加载systemjs.config.js,但应用程序未运行。

`script.src = "systemjs.config.js"; `script.src =“ systemjs.config.js”;

Can I load systemjs dynamically? 我可以动态加载systemjs吗?

This is how you should load System JS .. 这是您应该如何加载System JS ..

https://github.com/angular/quickstart/blob/master/src/index.html https://github.com/angular/quickstart/blob/master/src/index.html

This is taken from the Angular Quickstart project. 这取自Angular Quickstart项目。

I'm providing the entire index.html for context: 我为上下文提供了整个index.html:

!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart</title>
    <base href="/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>

    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <script src="systemjs.config.js"></script>
    <script>
      System.import('main.js').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>Loading AppComponent content here ...</my-app>
  </body>
</html>

SystemJS is loaded up front, not dynamically. SystemJS是预先加载的,不是动态加载的。 The job of System JS is a dynamic module loader but, since you don't have a module loader until System JS is loaded, you cannot load System JS itself dynamically. System JS的工作是动态模块加载器,但是,由于在加载System JS之前没有模块加载器,因此无法动态加载System JS本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM