简体   繁体   English

Visual Studio 2017的Typescript支持

[英]Typescript Support for Visual Studio 2017

I have recently been trying to learn TypeScript, and in some of the videos I have been watching after installing the 'Web Essentials' extension when working with a .ts file visual studio gives you a pre-view panel so you can see the JavaScript as you type. 我最近一直在尝试学习TypeScript,在使用.ts文件安装“Web Essentials”扩展程序后,我一直在观看的一些视频中,visual studio为您提供了一个预览面板,因此您可以将JavaScript视为你输入。

These videos seem to be using VS 2012. 这些视频似乎正在使用VS 2012。

However installing Web Essentials on VS 2017 doesn't seem to have this option, does anyone know how I can get this to work in VS 2017? 但是,在VS 2017上安装Web Essentials似乎没有这个选项,有谁知道如何在VS 2017中使用它? Does it have the same support for TypeScript? 它对TypeScript有相同的支持吗?

So TypeScript is installed by default with VS2017 (Update 2 will give you the latest version of 2.3 and allow side by side installs). 所以TypeScript默认安装在VS2017上(Update 2将为您提供最新版本的2.3并允许并排安装)。 If you don't have it you can add it via the Visual Studio Installer - from the installer click the burger menu and choose Modify, then click Individual Components and under SDKs, Libraries and Frameworks you can add TypeScript. 如果您没有它,可以通过Visual Studio Installer添加它 - 从安装程序中单击汉堡菜单并选择Modify,然后单击Individual Components,在SDKs,Libraries and Frameworks下可以添加TypeScript。

Once installed you can add a tsconfig.json file to tell the TypeScript compiler how to behave, example here: 安装完成后,您可以添加一个tsconfig.json文件来告诉TypeScript编译器如何操作,例如:

{
    "compileOnSave": true,
    "compilerOptions":
    {
        "noImplicitAny": false,
        "noEmitOnError": true,
        "removeComments": false,
        "sourceMap": true,
        "target": "es5"
    },
    "exclude":
    [
        "node_modules"
    ]
}

The important setting is compileOnSave which tells VS to compile the js file when you save the ts. 重要的设置是compileOnSave,它告诉VS在保存ts时编译js文件。

You should now see you have a nested Javascript file within your TypeScript file: 您现在应该在TypeScript文件中看到嵌套的Javascript文件:

嵌套的TypeScript

Now you just need to open both files side by side and when you save the ts then js will automatically update (it helps to tick the always update option if VS prompts you to load the changed file on disk). 现在你只需要并排打开这两个文件,当你保存ts时,js会自动更新(如果VS提示你在磁盘上加载更改的文件,它有助于勾选总是更新选项)。

I see your question is for VS 2017 and I don't find any documentation on VS 2017 to confirm if it has been added back. 我看到你的问题是VS 2017,我在VS 2017上没有找到任何文件来确认它是否已被添加回来。 However, I did find that this was removed in VS 2015. See this link on Github 但是,我确实发现这在VS 2015中已被删除。请参阅Github上的此链接

The TS preview pane has been removed from Web Essentials 2015 due to continuously running into conflicts with the TS compiler when new versions come out. 由于在新版本发布时与TS编译器发生冲突,TS预览窗格已从Web Essentials 2015中删除。 The TS team are aware of it and I hope they will add the feature into the TS tooling in the future. TS团队已经意识到这一点,我希望他们将来可以将这个功能添加到TS工具中。 It is possible that this feature will be included in the brand new Web Compiler extensions ( https://visualstudiogallery.msdn.microsoft.com/3b329021-cd7a-4a01-86fc-714c2d05bb6c ) when preview panes have been implemented. 当实现预览窗格时,此功能可能会包含在全新的Web编译器扩展( https://visualstudiogallery.msdn.microsoft.com/3b329021-cd7a-4a01-86fc-714c2d05bb6c )中。

I have a work around, it is a bit of work but helps with the odd occasions when I want to check the output. 我有一个解决方法,这是一些工作,但有助于奇怪的场合,当我想检查输出。

I am running vs2017 I have web essentials installed and web compiler, but it still doesn't appear to support ts as of 15.4 and I couldn't get the tsconfig.json to work. 我正在运行vs2017我已经安装了web essentials和web编译器,但是从15.4开始它似乎仍然不支持ts并且我无法使tsconfig.json工作。

Briefly show the hidden files in your project and select the corresponding js file, then drag the tab to the right so you have a split screen 简要显示项目中的隐藏文件并选择相应的js文件,然后将选项卡向右拖动,这样就有了分屏 在此输入图像描述

When you change and save the ts file, you might get a popup saying "The file has changed" or something like that, there is a check box you can select on there which says "AutoRestore if no changes detected" (again I might be paraphrasing, I attempted to find the option so I could reset it to get a screenshot but I cannot find it!), check that and then select "Yes to All" this refreshes the js file. 当您更改并保存ts文件时,您可能会弹出一个“文件已更改”或类似内容的弹出窗口,您可以在其中选中一个复选框,其中显示“如果未检测到更改则自动恢复”(同样我可能是释义,我试图找到选项,所以我可以重置它以获得截图,但我找不到它!),检查,然后选择“是全部”这刷新js文件。 The next time you change your ts file the js file will update. 下次更改ts文件时,js文件将更新。

I know it isn't great, but I found it very useful as I could get an analog of what you see when following the course and they keep showing the preview of the javascript. 我知道它不是很好,但我发现它非常有用,因为我可以得到你在阅读课程后看到的内容,并且他们会继续显示javascript的预览。

In Visual Studio 2017, you need to install the "Node.js development" Workload during the installation process or modify your existing install in the New Project dialog by clicking the "Not finding what you're looking for? Open Visual Studio Installer" link. 在Visual Studio 2017中,您需要在安装过程中安装“Node.js development”工作负载,或者在“新建项目”对话框中修改现有安装,方法是单击“找不到您要查找的内容?打开Visual Studio Installer”链接。

Then File > New > Project... will include Javascript and Typescript project templates under the Installed category. 然后File> New> Project ...将在Installed类别下包含Javascript和Typescript项目模板。

I have installed web essentials 2017, but not sure if that is mandatory. 我已经安装了web essentials 2017,但不确定这是否是强制性的。 The question is how to open two files side by side in VS 2017; 问题是如何在VS 2017中并排打开两个文件; specifically a typescript and its corresponding js file. 特别是一个打字稿及其对应的js文件。

You can open any two files side by side the steps for which are as follows. 您可以并排打开任意两个文件,步骤如下。

First open the two files in question by double clicking them from Solution Explorer. 首先在解决方案资源管理器中双击它们,打开有问题的两个文件。

Visual Studio 2017分割窗口步骤1

Next, place your cursor on the highlighted red box show above and drag that Car.js file tab down. 接下来,将光标放在上面突出显示的红色框上,然后将该Car.js文件选项卡向下拖动。

Visual Studio 2017分割窗口步骤2

Now you see five small squares, highlighted with red square box. 现在你看到五个小方块,用红色方框突出显示。 Move your cursor on any of the 4 outer small squares. 将光标移动到4个外部小方块中的任何一个上。 I am placing my cursor on the right one as shown below. 我将光标放在右边,如下所示。

Visual Studio 2017分割窗口步骤2

There you go. 你去吧

I guess this works without web essentials and is a feature of vs. Let me know if you could do this without having web essentials installed. 我想这可以在没有网络必需品的情况下运行,并且是一个功能。让我知道你是否可以在没有安装网络必需品的情况下做到这一点。

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

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