首页 > 解决方案 > MVC .Net 角度未启动

问题描述

我正在使用 mvc .Net 结合最新的 angular(5.2.10)。问题是我无法启动并运行 Angular。

Angular 应用程序文件位于“应用程序”文件夹下。'_Layout.cshtml' 位于 Inside Views/Shared 文件夹中。

_Layout.cshtml:

<!DOCTYPE html>
<html>
<head>
   <base href="/" />
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">

   <!-- Polyfill(s) for older browsers -->
   <script type="text/javascript" src="~/node_modules/core-js/client/shim.min.js"></script>
   <script type="text/javascript" src="~/node_modules/zone.js/dist/zone.js"></script>
   <script type="text/javascript" src="~/node_modules/systemjs/dist/system.src.js"></script>

   <script type="text/javascript" src="~/systemjs.config.js"></script>
   <script type="text/javascript">
    System.import('app/main.js').catch(function (err) { console.error(err); });
</script>

</head>

<body>           
   <div>    
       @RenderBody()
   </div>
</body>
</html>

systemjs.config.js:

  (function (global) {
  System.config({
    paths: {
       // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
map: {
  // our app is within the app folder
  'app': './app',

  // angular bundles
  '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
  '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
  '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
  '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
  '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
  '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
  '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
  '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

  // other libraries
  'rxjs':                      'npm:rxjs',
  'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
  app: {
    defaultExtension: 'js',
    meta: {
      './*.js': {
        loader: "systemjs-angular-loader.js"
      }
    }
  },
  rxjs: {
          defaultExtension: 'js'
        }
     }
  });
  })(this);

我得到的错误是:

Error: Fetch error: 404 Not Found (systemjs-angular-loader.js)
  Instantiating http://www.nlgdevelopment.com/systemjs-angular-loader.js
  Loading http://www.nlgdevelopment.com/systemjs-angular-loader.js
  Instantiating http://www.nlgdevelopment.com/app/main.js
  Loading app/main.js
     at fetch.js:37
     at ZoneDelegate.invoke (zone.js:388)
     at Zone.run (zone.js:138)
     at zone.js:872
     at ZoneDelegate.invokeTask (zone.js:421)
     at Zone.runTask (zone.js:188)
     at drainMicroTaskQueue (zone.js:595)

'Systemjs-angular-loader.js' 和 'systemjs.config.js' 文件位于同一级别(根路径)。我已按照官方 Angular 网站提供的快速入门指南进行操作,但出现了问题。

标签: angular

解决方案


如果这是一个新项目,我建议删除它并使用 dot net 命令开始一个新项目:

dotnet new angular -o my-new-app

这将使用 Angular CLI 生成您的应用程序。基本上,您的 MVC 应用程序根本不包含任何“视图”文件夹,甚至不包含 _Layout.cshtml 页面。所有客户端逻辑和模板都存储在“Client App”文件夹中。


推荐阅读