【发布时间】:2016-07-29 18:35:10
【问题描述】:
我正在尝试为使用 react 用 typescript 编写的应用程序设置开发环境。 我确实有可以编译为 ES5 和 AMD 模块的现有 typescript 代码。
我想避免在浏览器中进行任何 js 转换。请注意,我不需要使用 babel 来转译 jsx,因为 typescript 会将标签编译到 tsx 文件中的 React.createElement 中。
我照常做:
npm install react -save
npm install react-dom -save
npm install typescript -save-dev
npm install typings -save-dev
typings install react --ambient --save
typings install react-dom --ambient --save
现在,我有两个问题:
-
如何在我的 tsx 文件中正确导入/要求 react 和 react-dom 编译为 ES5/AMD?
目前我只是这样做:
/// <reference path="../../typings/browser/ambient/react-dom/index.d.ts" /> /// <reference path="../../typings/browser/ambient/react/index.d.ts" /> export class MyComponent extends __React.Component<any, {}> { public render() { return ( <h1>MyCompoentnt</h1> ); } }但是编译失败并出现错误 TS2304: Cannot find name 'React'。
-
如何在我的应用程序中包含 react.js 和 react-dom.js? 我想需要一些转译或浏览器化。还是我应该和他们一起去 index.html?
<script src="scripts/react.js"></script> <script src="scripts/react-dom.js"></script> <script src="scripts/require.min.js" data-main="scripts/app"></script>
这是我的 package.json:
{
"name": "myhtmlapp",
"version": "1.0.0",
"description": "react test app",
"main": "index.js",
"dependencies": {
"jquery": "^2.2.3",
"react": "^15.0.0",
"react-dom": "^15.0.0"
},
"devDependencies": {
"browser-sync": "^2.11.2",
"typescript": "^1.8.9",
"typings": "^0.7.12"
},
"scripts": {
"watch:typescript": "tsc --p ./appscripts -w",
"watch:css": "browser-sync start --server --files .wwwroot/css/*.css",
"compile": "tsc --p ./appscripts",
"test": "echo \"Error: no test specified\" "
},
"keywords": [],
"author": "",
"license": "ISC"
}
【问题讨论】:
标签: reactjs typescript npm