visual studio typescript“Uncaught ReferenceError:exports not not at ....”。 [英] visual studio typescript "Uncaught ReferenceError: exports is not defined at...."

查看:834
本文介绍了visual studio typescript“Uncaught ReferenceError:exports not not at ....”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用visual studio学习typeScript并试图做一个简单的类导出。我多次看到这个问题,但没有任何解决方案对我有帮助。我做错了什么?




  • 我已将CommonJs的模块系统更改为系统

  • 安装npm systemJs

  • 尝试使用import来代替/// ... reference path .... /


仍然出现同样的错误Uncaught ReferenceError:exports not defined at ...

  import {Address} from./address\";class Customer {protected name:string =; public addressObj:Address = new Address(); private _CustomerName:string =; public set CustomerName(value:string){if(value.length == 0){throwCustomer name is requaierd} this._CustomerName = value; } public get CustomerName():string {return this._CustomerName; }}  

export class Address {public street1:string = ; }

<!doctype html>< html>< ; HEAD> <标题>< /标题> < meta charset =utf-8/>< / head>< body> < script src =address.js>< / script> < script src =Customer.js>< / script> <脚本>尝试{cust = new Customer(); cust.CustomerName =doron; cust.addressObj.street1 =test} catch(ex){alert(ex); $ lt; / body>< / html>

我还没做什么?!?!

解决方案

我刚刚解决了这个问题。或者说,我发现了一篇博客文章,其中包含



Greeter.ts



  export class Greeter {
元素:HTMLElement;
span:HTMLElement;
timerToken:number;

构造函数(元素:HTMLElement){
this.element = element;
this.element.innerText + =时间是:;
this.span = document.createElement('span');
this.element.appendChild(this.span);
this.span.innerText = new Date()。toUTCString();
}

start(){
this.timerToken = setInterval(()=> this.span.innerText = new Date().UTUTCString(),500);
}

stop(){
clearTimeout(this.timerToken);
}

}



AppConfig.ts

从./AppMain

require(['AppMain'],
')导入{AppMain}

  (main:any)=> {
var appMain = new AppMain();
appMain.run();
}
);



AppMain.ts



 从./classes/Greeter导入{Greeter} 

导出类AppMain {
public run(){
// window.onload
var dummyEl:HTMLElement = document.createElement('span');
var theEl:HTMLElement | null = document.getElementById('content');;
var el:HTMLElement = theEl!== null? theEl:dummyEl;
var greeter:Greeter = new Greeter(el);
greeter.start();
}
};

或使用:

  var theEl:HTMLElement = document.getElementById('content'); 
var greeter:Greeter = new Greeter(theEl);

只要意识到所谓的'错误'就是一个警告! / p>

  app / AppMain.ts(7,13):error TS2322:Type'HTMLElement | null'不可分配为键入'HTMLElement'。 
类型'null'不可分配以键入'HTMLElement'。



app.ts



不再使用

index.html



 < html lang =en> 
< head>
< meta charset =utf-8/>
< title> TypeScript HTML App< / title>
< link rel =stylesheethref =app.csstype =text / css/>
<! -
< script type =text / javascriptsrc =app / classes / Greeter.js>< / script>
< script src =app.js>< / script>
- >
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆