asp.net - Angular2/ASP.NET - "No ResourceLoader implementation has been provided. Can' t 读取 URL"

标签 asp.net visual-studio angular visual-studio-2015

我正在尝试在 Visual Studio 上构建我自己的 Angular 2/ASP.NET SPA。您可以找到所有文件 here .

我尝试做的很简单:按照说明设置应用程序后 here ,我开始添加自己的文件以尝试进行一些基本路由,并删除了一些多余的文件。我有 ClientApp/app/app.module.ts 引导 ClientApp/app/components/app/app.component.ts,唯一的路径是 ClientApp/应用程序/组件/应用程序/test.component.ts

不幸的是,我遇到了这个错误:

An unhandled exception occurred while processing the request.

Exception: Call to Node module failed with error: Error: No ResourceLoader implementation has been provided. Can't read the url "app.component.html"
at Object.get (C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:17454:17)
at DirectiveNormalizer._fetch (C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:13455:45)
at DirectiveNormalizer.normalizeTemplateAsync (C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:13498:23)
at DirectiveNormalizer.normalizeDirective (C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:13473:46)
at RuntimeCompiler._createCompiledTemplate (C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:16869:210)
at C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:16807:43
at Array.forEach (native)
at C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:16805:50
at Array.forEach (native)
at RuntimeCompiler._compileComponents (C:\Users\Student\Source\Workspaces\mvs\Test2\Test2\node_modules\@angular\compiler\bundles\compiler.umd.js:16804:45)

我觉得一切正常,但我是 Angular 2 和 ASP.NET 的新手,所以我不知道这是编译器问题还是人为错误。这是一些代码,如果您需要更多信息,则 repo 链接位于此问题的顶部。

ClientApp/app/app.module.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UniversalModule } from 'angular2-universal';
import { AppComponent } from './components/app/app.component';
import { TestComponent } from './components/app/test.component';

@NgModule({
    bootstrap: [ AppComponent ],
    declarations: [
        AppComponent,
        TestComponent
    ],
    imports: [
        UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
        RouterModule.forRoot([
            { path: 'test', component: TestComponent },
            { path: '', redirectTo: 'test', pathMatch: 'full' },
            { path: '**', redirectTo: 'test' }
        ])
    ]
})
export class AppModule {
}

ClientApp/app/components/app/app.component.ts

import { Component, Input } from '@angular/core';

@Component({
    selector: 'app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
}

ClientApp/app/components/app/test.component.ts

import {Component, Input, OnInit} from '@angular/core';

@Component({
    templateUrl: './test.component.html',
    styleUrls: ['./test.component.css']
})

export class TestComponent implements OnInit {
    testing: String;

    ngOnInit(): void {
        this.testing = "This Works";
    }
}

最佳答案

UniversalModule 目前要求模板和样式的外部资源使用 require()。

尝试将 templateUrl: './test.component.html' 更改为 template: require('./test.component.html')

styleUrls: ['./test.component.css']styles: [require('./test.component.css')]

可在此处找到有关此问题的更多详细信息:Unable to load assets without using require()

作为引用,这里是 Github 上来自 Angular 的问题线程:track runtime styleUrls .他们推荐使用 angular2-template-loader。

关于asp.net - Angular2/ASP.NET - "No ResourceLoader implementation has been provided. Can' t 读取 URL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41797199/

相关文章:

c++ - 为什么创建 std::runtime_error 会重置 WSAGetLastError?

.net - 将 PackageReference 用作 RestoreProjectStyle 时,如何避免传递依赖项的类型暴露?

visual-studio - 代码分析/FxCop CA1726 :UsePreferredTerms showing Cancelled as Deprecated

angular - 在新文件夹中运行 npm install 后 ng build --prod build 失败

c# - ASP.NET 用户控件设计器支持

c# - 重新加载另一个用户的页面

javascript - AngularJS 实现 ui-grid

asp.net - Asp.net 中的 Windows Workflow 4

javascript - 如何在angular2中使用 ‘Rx Observable’来解决双击问题

angular - 是否可以从应用程序代码中以 Angular 强制硬应用程序更新(Ctrl + F5 浏览器模拟)?