繁体   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