简体   繁体   English

如何将 javascript 文件添加到 Visual Studio Community 2019 typescript 项目

[英]How add javascript files to Visual Studio Community 2019 typescript project

I found some tutorial that explains how to build the simplest project: Tutorial: Create a Node.js and React app in Visual Studio我找到了一些解释如何构建最简单项目的教程: 教程:在 Visual Studio 中创建 Node.js 和 React 应用程序

I succeeded to build a project that shows a message "Welcome to React.:".我成功构建了一个显示消息“Welcome to React.:”的项目。 This project contains only one file in TypeScript.该项目仅包含 TypeScript 中的一个文件。 app.tsx.应用程序.tsx。 Now I want to build a project containing some additional modules written in React and having extension ".js".现在我想构建一个项目,其中包含一些用 React 编写的附加模块并具有扩展名“.js”。 It seems that these files should be transpiled to TypeScript.看来这些文件应该被转译成TypeScript。 But I did not find how to do that in this project.但我没有找到如何在这个项目中做到这一点。 For example in app.tsx there is a code例如在 app.tsx 中有一段代码

import Button from "./Button";
...
ReactDOM.render(<Button />, document.getElementById('root'));

and I have file Button.js that contains我有文件 Button.js 包含

...
handleClick = () => {
this.props.clickHandler(this.props.name);
};

When I run in command line当我在命令行中运行时

node_modules\.bin\webpack app.tsx --config webpack-config.js

I get an error message:我收到一条错误消息:

ERROR in ./Button.js 11:14
Module parse failed: Unexpected token (11:14)
You may need an appropriate loader to handle this file type, 
currently no loaders are configured to process this file. See 
https://webpack.js.org/concepts#loaders
|   //};
|
>   handleClick = () => {
|     this.props.clickHandler(this.props.name);
|   };

In npm folder I have ts-loader, so I do not understand what the problem?在 npm 文件夹中我有 ts-loader,所以我不明白是什么问题? Update 1. I did changes as Rich N suggested and now it works.更新 1. 我按照 Rich N 的建议进行了更改,现在它可以工作了。 But I still want to use the original content of Button.js:但是我还是想用Button.js的原始内容:

import React from "react";
import PropTypes from "prop-types";
//import "./Button.css";

export default class Button extends React.Component {
static propTypes = {
name: PropTypes.string,    
clickHandler: PropTypes.func,
};

handleClick = () => {
this.props.clickHandler(this.props.name);
};

setImage= () => {
  var img="./Images/";
 switch(this.props.name)
 {
   case "move":
       img+="Move.jpg";
       break;
    case "drawPoint":
       img+="Point.jpg";
       break;
       default:
           console.log("default image")
 }
 return img;
 };

 render() {


 return (
  <div >
    <button onClick={this.handleClick}>
        <img src={this.setImage()} alt=""></img>
   </button>
  </div>
  );
  }
 }

When I try to run the project I receive a number of error messages.当我尝试运行该项目时,我收到了许多错误消息。 For example:例如:

Error   TS2339  (TS) Property 'props' does not exist on type 'Button'.

I think this is because not each JavaScript code is valid TypeScript code.我认为这是因为不是每个 JavaScript 代码都是有效的 TypeScript 代码。 So I still have 2 questions:所以我还有2个问题:

  1. How change this code so that it will be right TypeScript code?如何更改此代码以使其正确 TypeScript 代码?
  2. Is there any utility that allows to do that automatically, not manually?是否有任何实用程序允许自动而不是手动执行此操作?

You're right, if you just want to use this code trying to do it in a TypeScript project is a pain.没错,如果您只是想在 TypeScript 项目中尝试使用此代码,那是很痛苦的。 You either need to make some changes so props and its children are properly typed for TypeScript, or change the imports so they're not typed.您要么需要进行一些更改,以便为 TypeScript 正确键入 props 及其子项,要么更改导入以便不键入它们。 Working to make the props properly typed seems pointless since the code is already using prop-types to type them.努力使 props 正确键入似乎毫无意义,因为代码已经使用 prop-types 来键入它们。

However, changing the imports isn't actually too hard.但是,更改导入实际上并不太难。 Just create Button.tsx with your Button.js code as in my original answer, and then replace the import React from "React" and import PropTypes from "prop-types" statements with the code below.只需像我原来的答案一样使用您的 Button.js 代码创建 Button.tsx,然后用下面的代码替换import React from "React"import PropTypes from "prop-types"语句。 You'll see this makes React and PropTypes of type 'any' if you hover over them, and means TypeScript won't try type-checking everything.如果你对它们进行 hover,你会看到这使得 React 和 PropTypes 类型为“any”,这意味着 TypeScript 不会尝试对所有内容进行类型检查。

declare var require: any
var React = require('react');
var PropTypes = require('prop-types');

Note that you need the npm package prop-types installed for this to work.请注意,您需要安装 npm package 道具类型才能工作。 If I do that I can display the button with the code below in app.tsx:如果我这样做,我可以在 app.tsx 中显示带有以下代码的按钮:

ReactDOM.render(<Button name="move"
    clickHandler={(x) => alert('Move')} />, document.getElementById('root'));

Whether you'll have other problems with TypeScript isn't clear though. TypeScript 是否还有其他问题尚不清楚。 A better alternative may be to turn this into a JavaScript project .更好的选择可能是将其变成 JavaScript 项目 To that you need to change the.tsx files to.jsx and to use a Babel loader in the Webpack config to compile them.为此,您需要将 .tsx 文件更改为 .jsx 并在 Webpack 配置中使用 Babel 加载器来编译它们。 If you want to do this I can explain how in more detail: I actually did it as an exercise just now so I have working code.如果你想这样做,我可以更详细地解释一下:我实际上是作为练习做的,所以我有工作代码。

There's no utility to do this automatically, in answer to your second question.在回答您的第二个问题时,没有实用程序可以自动执行此操作。

I'm leaving my original answer below.我将在下面留下我的原始答案。

ORIGINAL ANSWER原始答案

I struggled a bit to work out what you're doing here.我有点费力地弄清楚你在这里做什么。 However, it looks like you are trying to create a React 'Button' component in a plain.js file in the tutorial example.但是,您似乎正在尝试在教程示例中的 plain.js 文件中创建 React 'Button' 组件。 This isn't going to work.这是行不通的。 The tutorial has set up Webpack to transpile TypeScript to JavaScript, and a React component therefore needs to be in a.tsx file.本教程设置了 Webpack 以将 TypeScript 转换为 JavaScript,因此需要在 a.tsx 文件中添加 React 组件。

The steps below will get you to a Button component that works:以下步骤将带您进入一个有效的 Button 组件:

  • Rename Button.js to Button.tsx将 Button.js 重命名为 Button.tsx
  • Then change the code to:然后将代码更改为:

Button.tsx按钮.tsx

declare var require: any
var React = require('react');

var buttonStyle = { background: 'red' };

export default class Button extends React.Component {
    render() {
        return (
        <button
            style = { buttonStyle }
            onClick = { this.props.handleClick } > { this.props.label }</button>
            );
    }
}

app.tsx应用程序.tsx

declare var require: any
import Button from "./Button";
var React = require('react');
var ReactDOM = require('react-dom');

//export class Hello extends React.Component {
//    render() {
//        return (
//            <h1>Welcome to React!!</h1>
//        );
//    }
//}

//ReactDOM.render(<Hello />, document.getElementById('root'));
ReactDOM.render(<Button label="Click Me"
    handleClick={() => alert('Hello')} />, document.getElementById('root'));

By the way, it would help if you included all your code in your questions so we can easily see what your problem is.顺便说一句,如果您将所有代码都包含在您的问题中,这将有所帮助,这样我们就可以轻松地看到您的问题是什么。 In this case all you need to show is app.tsx and Button.js, and we'd have a minimal reproducible example .在这种情况下,您需要展示的只是 app.tsx 和 Button.js,我们将有一个最小的可重现示例

暂无
暂无

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

相关问题 如何在使用 Visual Studio 2019 制作的 Django 项目中添加 TypeScript 文件 - How do I add TypeScript files in a Django project made with Visual Studio 2019 是否可以在Visual Studio 2019中使用TypeScript创建用于开发javascript库的项目? - Is it possible to create project for developing javascript library with TypeScript in Visual Studio 2019? 如何在Visual Studio Community 2015中调试独立的JavaScript文件? - How can I debug stand alone JavaScript files in Visual Studio Community 2015? Visual Studio 2019 社区中适用于 JavaScript 的通用 Windows 平台 (UMP) 在哪里? - Where is Universal Windows Platform (UMP) for JavaScript within Visual Studio 2019 Community? Visual Studio 2019 的 React Redux 项目模板,JavaScript 问题 - React Redux Project template by Visual Studio 2019, JavaScript question Visual Studio 2019 Javascript - 如何引用应用程序根相对路径? - Visual Studio 2019 Javascript - how to reference application root relative path? 在Visual Studio中使用“网站项目”调试Typescript(或JavaScript) - Debug Typescript (or JavaScript) with a “Web Site Project” in Visual Studio 如何使用visual studio 2019和javascript从api获取数据? - How to get data from api using visual studio 2019 and javascript? Visual Studio 2012:将更多TypeScript文件编译为一个JavaScript文件 - Visual Studio 2012: compile more TypeScript files to one JavaScript file Visual Studio 2013没有找到从typescript编译的javascript文件? - Visual Studio 2013 not finding javascript files compiled from typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM