简体   繁体   English

angular 2调整System.config()后单独的javascript文件和打字稿文件

[英]angular 2 adjust System.config() after separate javascript files and typescript files

My project folder structure is the same as quick-start ToH https://angular.io/docs/ts/latest/tutorial/toh-pt1.html 我的项目文件夹结构与快速入门ToH https://angular.io/docs/ts/latest/tutorial/toh-pt1.html相同

So to separate .ts file and .js file, I added to the tsconfig.json with "outDir": "dist" . 因此,为了分隔.ts文件和.js文件,我使用"outDir": "dist"添加到tsconfig.json。 tsc then compiled all the .js and .map files into the dist folder. tsc然后将所有.js和.map文件编译到dist文件夹中。 So far so good. 到现在为止还挺好。 dist folder is parallel to app folder. dist文件夹与app文件夹并行。

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "outDir": "dist",
    "suppressImplicitAnyIndexErrors":true
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
} 

Next I changed the system.import in index.html 接下来我更改了index.html中的system.import

<script>
  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      },
      services: {defaultExtension: 'js'}
    }
  });
  //System.import('app/main')
  System.import('./dist/app/main')
        .then(null, console.error.bind(console));
</script>

Now after npm start, the browser gave me 现在npm开始后,浏览器给了我

 system.src.js:1154 GET http://localhost:3000/dist/app/main 404 (Not Found)

How can I let system import function find the main.js? 如何让系统导入功能找到main.js?

The folder structure looks like this 文件夹结构如下所示

root
  |--app            // all .ts files are here
  |--dist
       |--app       // all .js and .map files are here

I would leverage a map block if you want to keep the main HTML file into the root folder: 我会充分利用map ,如果你想在主HTML文件保存到根文件夹块:

<script>
  System.config({
    map: {
      app: 'dist/app' // <-----
    },
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      },
      services: {defaultExtension: 'js'}
    }
  });
  System.import('app/main')
        .then(null, console.error.bind(console));
</script>

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

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