【问题标题】:Use angular2 with visual studio将 angular2 与视觉工作室一起使用
【发布时间】:2015-12-19 03:54:55
【问题描述】:

有没有人让 Angular2(测试版)在 Visual Studio 中工作?

我正在使用带有更新 1 的 Visual Studio 2015,并且我有 n 个 HTMLTypeScript 项目。

如果我将“模块系统”设置为“系统”,则会出现构建错误(例如:找不到模块 'angular/core'),但它会在保存时编译 ts 文件(而不是在构建时)。如果我已经运行了应用程序,我可以刷新页面并且它可以工作。

如果我将“模块系统”设置为“CommonJS”,它将正确构建,但会出现运行时错误,例如“'require' is undefined”和“Unable to get property 'split' of undefined or null reference”。

我认为我应该使用 system,因为这是在使用 tsconfig.json 和教程中使用的 lite-server(带有 npm start)时起作用的方法。

【问题讨论】:

  • 我在 VS 2013 的问题上遇到了问题:stackoverflow.com/questions/34366928/… 我记得我在 Intellij 中使用它时遇到了这个问题。当我在 tsconfig.json 中使用 TS 1.6 和 Exclude 时,该解决方案自行解决。
  • 我使用的是 1.7.3,问题不在于节点模块,而在于我编写的 TS(来自 angualr2 教程)。我认为不需要排除这个,它需要编译。
  • 是的,我也尝试使用 Visual Studio 使用 Angular 2,但在奇怪的行为和多个“布尔 sh*t”错误以及 8 小时的尝试解决之后,我只是转移删除了我的项目...

标签: typescript visual-studio-2015 angular


【解决方案1】:

我有 angular2 使用 Visual Studio 2015。我必须安装 .NET 5 RC1 来支持 tsconfig.json。他们有一个全新的项目结构,我非常喜欢。这是我的设置:

您会注意到我将我的 app 文件夹和 ts 文件放在我的 wwwroot 中 - 这只是我的偏好,它遵循 angular2 快速入门。我将捆绑的文件从我的节点模块复制到我 wwwroot 内的脚本文件夹中。

我基于 angular2 beta 快速入门创建了一个 package.json:

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  }
}

然后我右键单击 package.json 并选择“恢复包”并运行 NPM 安装以创建包含您看到的所有包的 node_modules 文件夹。然后我像这样设置我的 tsconfig.json 并将其放在根目录中:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}

这是让它发挥作用的关键。实际上,我必须重新加载解决方案才能看到 tsconfig.json 并开始使用它。除此之外,我进入我的工具 > 选项并设置打字稿选项,如下所示:

接下来我添加了我的 index.html 文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Sample Angular 2 App</title>
</head>
<body>
    <my-app>Loading...</my-app>

    <script src="scripts/angular2-polyfills.js"></script>
    <script src="scripts/system.src.js"></script>
    <script src="scripts/Rx.js"></script>
    <script src="scripts/angular2.dev.js"></script>
    <script>
        System.config({
            packages: {
                app: {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });

        System.import('app/boot').then(null, console.error.bind(console));
    </script>
</body>
</html>

脚本包含的顺序很重要!这是我的 MyApp.ts 文件:

import { Component } from "angular2/core";

@Component({
    selector: "my-app",
    template: `
        <div>Hello from Angular 2</div>
    `
})
export class MyApp {
    public constructor() {
    }
}

还有我的 boot.ts 文件:

import { bootstrap } from "angular2/platform/browser";
import { MyApp } from "./MyApp";

bootstrap(MyApp);

此时我觉得我已经准备好运行它了,所以我启动它并导航到我的 /index.html - 但我看到的只是“Hello World!”。它似乎忽略了我为 index.html 提供服务的路径,而是静态地写出文本。查看Startup.cs,我发现了问题。:

namespace Angular2Demo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();

            // Add this!
            app.UseStaticFiles();

            // Remove this!
            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

通过注释掉app.Run 并添加app.UseStaticFiles();(这需要安装一个额外的包),我准备再试一次:

成功了!

【讨论】:

  • 我曾尝试过这种方法,但由于 node_modules 的去向(高于 wwwroot)而放弃了。我会重新审视它。谢谢。
  • 从我的 html 页面引用节点 js 文件意味着在部署时我要么必须在部署时维护 node_modules 目录结构,要么部署整个高达 40+MB 的 node_modules。我不喜欢这种方法,所以我将我需要的角度引用复制到我的脚本文件夹中,以便只部署我需要的那些。
  • 我必须单击“IIS Express”旁边的箭头 --> CLR 类型 (.NET Framework) --> 并选择“.NET Framework”,然后才能使上述示例正常工作。 +1 @Zack 解决方案!!
  • 我还添加了:app.UseDefaultFiles();在 app.UseStaticFiles(); 之前所以我不必在末尾添加 /index.html - docs.asp.net/en/latest/fundamentals/static-files.html
  • ..但仍然必须清理项目,重建它,然后构建它以查看我的更改......简而言之:清理 - 多次构建直到它工作......
猜你喜欢
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2017-06-01
  • 2019-08-27
  • 1970-01-01
  • 2019-05-15
  • 2018-06-11
相关资源
最近更新 更多