【问题标题】:Typescript (or angular2) Error: No provider for RuntimeMetadataResolverTypescript(或 angular2)错误:没有 RuntimeMetadataResolver 的提供程序
【发布时间】:2017-04-08 15:29:48
【问题描述】:

我是 Typescript/Angular2 语言的新人。我正在尝试从 Typescript 书中开发示例应用程序,但我总是收到以下错误:

No provider for RuntimeMetadataResolver (ComponentResolver -> RuntimeCompiler -> RuntimeMetadataResolver)

在浏览器控制台中。 我认为问题出在packages.json中的组件版本中。 Angular2 已经使用 npm install 安装,所以版本是 2.0beta17。

堆栈跟踪:

BaseException@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:25797:23 [angular]
AbstractProviderError@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:26406:9 [angular]
NoProviderError@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:26442:9 [angular]
ReflectiveInjector_.prototype._throwOrNull@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27967:19 [angular]
ReflectiveInjector_.prototype._getByKeyDefault@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27995:20 [angular]
ReflectiveInjector_.prototype._getByKey@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27958:20 [angular]
ReflectiveInjector_.prototype._getByReflectiveDependency@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27948:16 [angular]
ReflectiveInjector_.prototype._instantiate@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27845:31 [angular]
ReflectiveInjector_.prototype._instantiateProvider@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27817:20 [angular]
ReflectiveInjector_.prototype._new@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27806:16 [angular]
ReflectiveInjectorDynamicStrategy.prototype.getObjByKeyId@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27461:36 [angular]
ReflectiveInjector_.prototype._getByKeyDefault@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27986:23 [angular]
ReflectiveInjector_.prototype._getByKey@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27958:20 [angular]
ReflectiveInjector_.prototype._getByReflectiveDependency@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27948:16 [angular]
ReflectiveInjector_.prototype._instantiate@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27845:31 [angular]
ReflectiveInjector_.prototype._instantiateProvider@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27817:20 [angular]
ReflectiveInjector_.prototype._new@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27806:16 [angular]
ReflectiveInjectorDynamicStrategy.prototype.getObjByKeyId@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27461:36 [angular]
ReflectiveInjector_.prototype._getByKeyDefault@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27986:23 [angular]
ReflectiveInjector_.prototype._getByKey@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27958:20 [angular]
ReflectiveInjector_.prototype.get@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:27767:16 [angular]
coreLoadAndBootstrap/<@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:36582:33 [angular]
ApplicationRef_.prototype.run/<@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:36774:26 [angular]
NgZoneImpl/this.inner<.onInvoke@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:32701:32 [angular]
Zone.prototype.run@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:117:24 [<root> => angular]
NgZoneImpl.prototype.runInner@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:32732:60 [<root>]
NgZone.prototype.run@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:32968:51 [<root>]
ApplicationRef_.prototype.run@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:36772:9 [<root>]
coreLoadAndBootstrap@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:36581:12 [<root>]
bootstrap@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:68267:12 [<root>]
@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:68328:1 [<root>]
@file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:1:2 [<root>]
  index.js:45341:13
    BrowserDomAdapter.prototype.logError file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:45341:13
    ExceptionHandler.prototype.call file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:25704:13
    ApplicationRef_.prototype.run/< file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:36783:17
    ZoneDelegate.prototype.invoke file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:233:17
    NgZoneImpl/this.inner<.onInvoke file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:32701:32
    ZoneDelegate.prototype.invoke file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:232:17
    Zone.prototype.run file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:117:24
    NgZoneImpl.prototype.runInner file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:32732:60
    NgZone.prototype.run file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:32968:51
    ApplicationRef_.prototype.run file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:36772:9
    coreLoadAndBootstrap file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:36581:12
    bootstrap file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:68267:12
    <anonimo> file:///C:/ProgettiWeb/TestAngular/Chapter2/static/scripts/index.js:68328:1
    <anonimo>

包.json:

{
  "name": "weather-widget",
  "version": "1.0.0",
  "private": true,
  "description": "",
  "dependencies": {
    "angular2": "^2.0.0-beta.17",
    "es6-shim": "^0.35.1",
    "reflect-metadata": "^0.1.2",
    "rxjs": "^5.0.0-beta.6",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-sourcemaps": "^1.9.1",
    "gulp-typescript": "^3.1.3",
    "gulp-uglify": "^2.0.0",
    "small": "^0.2.7"
  }
}

lib/index.ts:

import "zone.js";
import "rxjs";
import "reflect-metadata";
import "es6-shim";

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

bootstrap(About).catch(err => console.error(err));

lib/about.ts

import { Component } from "angular2/core";

@Component({
    selector: "about-page",
    template: `
        <h2>About</h2>
        This widget shows the weather forecast of Utrecht.
        The next 24 hours are shown under 'Today' and the forecast of 24-48 hours ahead under 'Tomorrow'.
        `
})
export class About {

}

lib/tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "lib": ["es2015", "dom"]
    }
}

gulpfile.js

var gulp = require('gulp');
var typescript = require('gulp-typescript');
var small = require('small').gulp;
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');

var tsProject = typescript.createProject('lib/tsconfig.json', {
    typescript: require('typescript')
});

gulp.task('compile', function() {
    return gulp.src('lib/**/*.ts')
        .pipe(sourcemaps.init())
        .pipe(tsProject())
        .pipe(small('index.js', {
            externalResolve: ['node_modules'],
            globalModules: {
                "crypto": {
                    standalone: "undefined"
                }
            }
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('static/scripts'));
});
gulp.task('release', ['compile'], function() {
    return gulp.src('static/scripts/scripts.js')
        .pipe(uglify())
        .pipe(gulp.dest('static/scripts'));
});

gulp.task('default', ['compile']);

静态/index.html

<!DOCTYPE HTML>
<html>
    <head>
        <title>Weather</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div id="wrapper">
            <about-page>Loading..</about-page>
        </div>
        <script src="scripts/index.js" type="text/javascript"></script>
    </body>
</html>

然后我运行“gulp”,在浏览器中加载 index.html 并收到该错误。

我有一个动态创建的 node_modules 文件夹,其中包含一个 angular2 文件夹...

提前致谢。

编辑:我检查了我的 index.js 生成的文件和

var RuntimeMetadataResolver = (function () {

存在。 此外,

exports.COMPILER_PROVIDERS

包含

__small$_xxx.RuntimeMetadataResolver

EDIT2:npm ls 命令没有错误

【问题讨论】:

  • 很遗憾,您的书使用的是过时版本的 Angular 2;自 beta 17 以来发生了许多重大变化。考虑使用 official documentation 学习 Angular 2。
  • 但是我正在学习打字稿... Angular 版本是 2.0 beta 17,书中的示例使用相同的版本。我已经使用“npm install”安装了它。如何编辑我的文件?
  • 您遇到的错误是 Angular 2 特有的。为了您学习 TypeScript 的目的,我建议您从书中找到不同的示例,而不是过时的 Angular 2 示例。
  • 所以没有办法让它工作......其他样本也已经过时了。
  • 如果我是你..也许还书?也许是这样的:basarat.gitbooks.io/typescript/content/docs/… online 更适合您?

标签: angular typescript


【解决方案1】:

这似乎不是 Angular 的问题,也不是 TypeScript 的问题,但是模块捆绑器 Small 有一个错误。文件a/b 可以编译两次(在条件下),即a/ba\b。这只发生在 Windows 上。这破坏了 Angular 的一些功能。可以尝试更新到small@0.2.8吗?

【讨论】:

    【解决方案2】:

    尝试从 beta 升级到最新版本的 angular 并尝试。

    尝试使用“npm update”

    【讨论】:

      【解决方案3】:

      您应该根据documentation根模块类 中导入它。 在您的情况下,它应该是:

      @NgModule({
        imports:      [ BrowserModule, RuntimeMetadataResolver],
        declarations: [ AppComponent ],
        bootstrap:    [ AppComponent ]
      })
      export class AppModule { }
      

      【讨论】:

      • 根模块类在哪里?我正在关注一本书的样本。我有一个目录和相关文件
      • 我推荐 angular2 文档。那里写着什么是根模块类。
      • 我的书是关于打字稿的。我在学习 Angular 之前买了它。你能向我解释一下它与我的项目有什么关系(也就是使用 gulp)吗?也许我要编辑 node_modules/angular2 文件夹中的文件?我尝试创建包含您编写的代码并添加导入的 ts 文件。它没有用。你的回答对我没有帮助。你能改进它吗?谢谢
      • @Zimas:Emanuele 使用的是旧的 Angular 2 测试版,它不支持模块。
      • @MichaelLiu:我在上一篇文章中回答了你。
      猜你喜欢
      • 2016-03-16
      • 2017-03-01
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2018-02-10
      • 2017-06-12
      • 1970-01-01
      相关资源
      最近更新 更多