javascript - 系统配置 defaultExtension 不适用于 Django

标签 javascript angularjs django angular systemjs

我正在尝试使用 Django 后端移植 Angular 2 教程

这是我的html文件

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

    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="/static/main.js"></script>
    <script src="/static/node_modules/es6-shim/es6-shim.min.js"></script>
     <script src="/static/node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="/static/node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>   

    <script src="/static/node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="/static/node_modules/systemjs/dist/system.src.js"></script>
    <script src="/static/node_modules/rxjs/bundles/Rx.js"></script>
    <script src="/static/node_modules/angular2/bundles/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {        
          app: {
           format: 'register',
            defaultExtension: 'js'
           }
        }
      });
      System.import('/static/app/main')
            .then(null, console.error.bind(console));
    </script>
  </head>

 <!-- 3. Display the application -->
  <body>

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

  </body>
</html>

我发现 System.js 不工作

System.import('/static/app/main')

我必须使用

System.import('/static/app/main.js')

并手动将 .js 添加到我所有的非第三库导入中,以便 Angular 应用程序正常工作。

有趣的是,我不必将 .js 添加到

'angular2/core'
'angular2/platform/browser'

因为只要我为我编写的文件的所有导入手动添加 .js 扩展名,System.js 就会自动解析导入。

但是如果我设置

System.defaultJSExtensions = true;

我不必再将 .js 添加到我的文件中,但是 System.js 失去了在 node_modules 中导入所有库的能力,而是尝试使用默认的 django 目录

http://localhost:8000/myApp/angular2/platform/browser.js 

谁能给我一些指导?

谢谢

最佳答案

我认为您误解了 defaultJSExtensions 配置的内容。后者只允许在导入模块时添加 js 扩展:

System.defaultJSExtensions = true;

// requests ./some/module.js instead
System.import('./some/module');

如果模块之前未使用 System.register 显式注册,则适用。

angular2.dev.js 文件包含 Angular2 核心模块(通过 System.register 显式注册)。包含带有 script 元素的文件只会使它们可用于导入。

如果你想使用 node_modules/angular2 中 Angular2 的单个 JS 文件(例如 core.js,...),你需要这个 SystemJS 配置:

System.config({
  defaultJSExtensions: true,
  map: {
    angular2: 'node_modules/angular2/src',
    rxjs: 'node_modules/rxjs'
  },
  packages: {
    app: {
      defaultExtension: 'js',
      format: 'register'
    }
  }
});
System.import('app/boot')
  .then(null, console.error.bind(console));

上面重要的是 map block 告诉 SystemJS 在哪里可以找到名称以 angular2/ 开头的模块。

在这种情况下,无需导入 Angular2 捆绑的 JS 文件(angular2.min.js,...)。

关于javascript - 系统配置 defaultExtension 不适用于 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813233/

相关文章:

javascript - Service Worker 似乎没有更新

javascript - PropTypes 位置的 Redux 最佳实践

django - ModelForm clean_xxxx() 适用于 CharField,不适用于 URLField。 Django 1.5

javascript - AngularJS 如何动态添加 HTML 并绑定(bind)到 Controller

javascript - 使用 AngularJS 实现本地存储

python - Django 自定义信号

python - 与 iexact 一起使用时,Django get_or_create 无法设置字段

javascript - 未捕获的语法错误 : missing ) after argument list 5

javascript - 如何从网站获取JS链接

angularjs - Angular2 构建错误 - 找不到类型