簡體   English   中英

Typescript和SystemJS無法導入類

[英]Typescript and SystemJS can not import a class

我是Typescript和SystemJS的新手,我從這個簡單的例子開始,希望我的ts文件能夠即時發布。 我創建了一個index.html文件。

<html>
    <head>
        <script src="node_modules/systemjs/dist/system.js"></script>
        <script>
          SystemJS.config({ 
            transpiler: 'typescript',
            map: {
              'typescript': 'node_modules/typescript/lib/typescript.js'
            },
            packages: {
              src: {
                defaultExtension: 'ts'
              }
            } 
          })
          SystemJS.import('src/main').then(function(m){
             console.log(m);
          }, function(error){
             console.log(error);
          });
        </script>
     </head>

     <body>

     </body>
</html>

然后將這兩個文件放入src文件夾。

//main.ts

import {Person} from './person';
let person = new Person();
console.log(person.name);


//person.ts

export class Person {
    public name: string = 'David';
} 

現在我在控制台中看不到任何內容,沒有錯誤沒有輸出。 我已經發現問題出現在這一行。

import {Person} from './person';

因為如果我評論它,我會看到這個合理的錯誤。

錯誤:無效或意外的令牌

請幫助我,我應該在哪里注意?

你沒有錯過打字稿的腳本標簽嗎?

<script src="node_modules/typescript/lib/typescript.js"></script>

我相信是你用作樣本的來源。 他們明確提到了這種錯誤。

謝謝你的鏈接。 我以這種方式添加並配置了一個plugin-typescript,它開始工作正常。

      SystemJS.config({ 
        transpiler: 'ts',
        map: {
          'typescript': 'node_modules/typescript/lib',
          'ts': 'node_modules/plugin-typescript/lib/plugin.js'
        },
      packages: {
        "typescript": {
          "main": "typescript.js",
          "meta": {
            "typescript.js": {
              "exports": "ts"
            }
          }
        }
      }
      })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM