简体   繁体   English

ReactJS with Typescript - 找不到变量:require

[英]ReactJS with Typescript - Can't find variable: require

Good day, 美好的一天,

I am trying use ReactJS with Typescript. 我正在尝试将ReactJS与Typescript一起使用。 Everything looks good, but after starting the application error occurs: "Can't find variable: require" 一切看起来都不错,但在启动应用程序后出现错误:“找不到变量:require”

I not use node.js for server side rendering. 我不使用node.js进行服务器端渲染。

Script: 脚本:

/// <reference path="../typings/tsd.d.ts"/>

import * as React from 'react'
import * as ReactDom from 'react-dom'

interface Props {}
interface State {}

export class App extends React.Component<Props, State>{

    render(){
      return (<div>Hello</div>);
    }
}

ReactDom.render(<App/>, document.getElementById("content"));

and this compiled boot.js: 这个编译好的boot.js:

"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var React = require('react');
var ReactDom = require('react-dom');
var App = (function (_super) {
    __extends(App, _super);
    function App() {
        _super.apply(this, arguments);
    }
    App.prototype.render = function () {
        return (React.createElement("div", null, "Ahoj"));
    };
    return App;
}(React.Component));
exports.App = App;
ReactDom.render(React.createElement(App, null), document.getElementById("content"));

Error: 错误: 在此输入图像描述

You're going to need module resolution. 您将需要模块分辨率。 require is a form of loading a module into your code. require是一种将模块加载到代码中的形式。 It's usually used with NodeJS. 它通常与NodeJS一起使用。 If you want to use it in the browser, try Browserify. 如果要在浏览器中使用它,请尝试使用Browserify。

require is not supported out of the box by browsers. require不被浏览器支持开箱即用。 You are going to need a module loader. 您将需要一个模块加载器。 I recommend webpack, here is a browser quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html 我推荐webpack,这是一个浏览器快速入门: https ://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM