首先当我导入反应时,打字稿编译器控制台'找不到模块反应',然后我添加///,问题解决了。
但我只是发现如果我删除了参考评论,没有错误。
这是我的代码:
/** * Created by huagu on 2015/12/21. */ import * as ReactDOM from "react-dom"; import * as React from "react"; import FloatButton from "./FloatButton"; window.onload = function (event:Event) { var bd = document.querySelector('#bd'); var style:__React.CSSProperties = { width:'80px', height:'40px', background:'blue', position:'absolute', top:'100px', left:'10px' } var str:__React.CSSProperties = ''; var num:number = ''; var c = <span style={{fontSize:'14px'}}>button</span> var props = { content:c, click:function(){ console.log("FloatButton clicked"); }, style:style, allowSeriesClick:false, } ReactDOM.render(<FloatButton {...props}></FloatButton>, bd); }所以,我的问题是引用注释是否有必要?我不添加引用注释,但没有错误引用__React.CSSProperties,它在react.d.ts中声明。 如果没有必要,为什么“var str:__ React.CSSProperties ='';”没有错误?
at first when I import react, the typescript compiler console 'can not find module react',then I add /// , and the problem fixed.
but I just found if I remove the reference comment, there is no error.
this is my code:
/** * Created by huagu on 2015/12/21. */ import * as ReactDOM from "react-dom"; import * as React from "react"; import FloatButton from "./FloatButton"; window.onload = function (event:Event) { var bd = document.querySelector('#bd'); var style:__React.CSSProperties = { width:'80px', height:'40px', background:'blue', position:'absolute', top:'100px', left:'10px' } var str:__React.CSSProperties = ''; var num:number = ''; var c = <span style={{fontSize:'14px'}}>button</span> var props = { content:c, click:function(){ console.log("FloatButton clicked"); }, style:style, allowSeriesClick:false, } ReactDOM.render(<FloatButton {...props}></FloatButton>, bd); }so, my problem is whether reference comment is necessary?I don't add reference comment, but there is no error reference __React.CSSProperties, it was declare in react.d.ts. if it's not necessary, why there is no error with "var str:__React.CSSProperties = ''; "?
最满意答案
///<reference>相当于说“只要相信我的代码会在我的代码之前运行”,这通常是您可以放心假设的。 它适用于IDE,因为它是一个注释,根本不会影响代码执行。
import modulename ...使用commonjs或requirejs在运行前面的代码之前实际检索指定的模块。
如果你有一个与你需要的模块一起使用的依赖管理系统,那么最好使用它而不是前一个解决方案; 这样,在加载模块之前没有尝试访问模块的风险(即使99%的使用在代码加载之前甚至无法远程访问)
///<reference> is the equivalent of saying "Just trust me that this will have run before my code", which is often something you can safely assume. It's intended for IDEs, and since it's a comment, does not affect code execution at all.
import modulename ... uses either commonjs or requirejs to actually retrieve the specified module before running the proceeding code.
If you have a dependency-management system that works with the module you need, it's preferred to use that instead of the former solution; that way, there is no risk of trying to access a module before it's loaded (even if 99% of uses aren't even remotely possible for the code to see before it has loaded)
更多推荐
发布评论