【问题标题】:Unexpected value 'AnyComponent' declared by the module 'AppModule'模块“AppModule”声明的意外值“AnyComponent”
【发布时间】:2016-12-24 09:16:47
【问题描述】:

我正在使用 Angular2,但在尝试在同一个 Typescript 文件中使用两个类时遇到了这个问题。

在编译时没有给我任何错误,但是当我尝试执行页面时,console.log 给出了这个错误:

Error: BaseException@http://www.my.app/panel-module/node_modules/@angular/compiler//bundles/compiler.umd.js:5116:27
    CompileMetadataResolver</CompileMetadataResolver.prototype.getNgModuleMetadata/<@http://www.my.app/panel-module/node_modules/@angular/compiler//bundles/compiler.umd.js:13274:35
    CompileMetadataResolver</CompileMetadataResolver.prototype.getNgModuleMetadata@http://www.my.app/panel-module/node_modules/@angular/compiler//bundles/compiler.umd.js:13261:21
    RuntimeCompiler</RuntimeCompiler.prototype._compileComponents@http://www.my.app/panel-module/node_modules/@angular/compiler//bundles/compiler.umd.js:15845:28
    RuntimeCompiler</RuntimeCompiler.prototype._compileModuleAndComponents@http://www.my.app/panel-module/node_modules/@angular/compiler//bundles/compiler.umd.js:15769:36
    RuntimeCompiler</RuntimeCompiler.prototype.compileModuleAsync@http://www.my.app/panel-module/node_modules/@angular/compiler//bundles/compiler.umd.js:15746:20
    PlatformRef_</PlatformRef_.prototype._bootstrapModuleWithZone@http://www.my.app/panel-module/node_modules/@angular/core//bundles/core.umd.js:9991:20
    PlatformRef_</PlatformRef_.prototype.bootstrapModule@http://www.my.app/panel-module/node_modules/@angular/core//bundles/core.umd.js:9984:20
    @http://www.my.app/panel-module/app/main.js:4:1
    @http://www.my.app/panel-module/app/main.js:1:31
    @http://www.my.app/panel-module/app/main.js:1:2
    Zone</ZoneDelegate</ZoneDelegate.prototype.invoke@http://www.my.app/panel-module/node_modules/zone.js/dist/zone.js:332:20
    Zone</Zone</Zone.prototype.run@http://www.my.app/panel-module/node_modules/zone.js/dist/zone.js:225:25
    scheduleResolveOrReject/<@http://www.my.app/panel-module/node_modules/zone.js/dist/zone.js:586:53
    Zone</ZoneDelegate</ZoneDelegate.prototype.invokeTask@http://www.my.app/panel-module/node_modules/zone.js/dist/zone.js:365:24
    Zone</Zone</Zone.prototype.runTask@http://www.my.app/panel-module/node_modules/zone.js/dist/zone.js:265:29
    drainMicroTaskQueue@http://www.my.app/panel-module/node_modules/zone.js/dist/zone.js:491:26
    F/</g@http://www.my.app/panel-module/node_modules/core-js/client/shim.min.js:8:10016
    F/<@http://www.my.app/panel-module/node_modules/core-js/client/shim.min.js:8:10138
    a.exports/k@http://www.my.app/panel-module/node_modules/core-js/client/shim.min.js:8:14293

    Evaluating http://www.my.app/panel-module/app/main.js
    Error loading http://www.my.app/panel-module/app/main.js

下面是我的组件打字稿文件。

// MyComponent.ts

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

@Component({
  selector: 'my-app',
  templateUrl: '/path/to/view',
})

export class MyObject {
  id: number;
}


export class MyComponent {
   obj: MyObject;

   // unecessary code
}

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    仅供参考,我的情况是我有一些 js 逻辑,它可能是

    <my-tag [HTML]="sourcesHtml ? sourcesHtml[i] : ''">
    

    改为

    <my-tag [HTML]="sourcesHtml[i]">
    

    错误已被删除

    【讨论】:

      【解决方案2】:

      你必须改变你的类的顺序,那么在同一个文件中有多个类是没有问题的。 因为在您的情况下,decorator @Component 现在正用于您的课程MyObject 而不是MyComponent

      decorator @Component 需要直接在您的组件定义前面!!

      import { Component, OnInit } from '@angular/core';
      
      
      export class MyObject1 {
        id: number;
      }
      
      @Component({
        selector: 'my-app',
        templateUrl: '/path/to/view',
      })
      export class MyComponent {
         obj: MyObject;
      
         // unecessary code
      }
      
      export class MyObject2 {
        id: number;
      }
      

      【讨论】:

      • 谢谢,我固定在其他方式,但我不知道为什么我需要这样做。
      • 因为decorator @Component 需要在你的组件前面。没有这个装饰器,你的组件就不是真正的组件。否则它只是一个类。
      • 谢谢,所以在模块中使用之前应该先定义组件。
      • 我遇到了相反的问题;我想定义一个Directive,但使用的是Component 装饰器。谢谢!
      【解决方案3】:

      这是我修复它的方法:

      把MyObject放到一个单独的文件中,然后导入。

      // MyObject.ts

      export class MyObject {
        id: number;
      }
      

      // MyComponent.ts

      import { Component, OnInit } from '@angular/core';
      import { MyObject } from 'path/to/MyObject';
      
      @Component({
        selector: 'my-app',
        templateUrl: '/path/to/view',
      })
      
      export class MyComponent {
         obj: MyObject;
      
         // unecessary code
      }
      

      【讨论】:

      • 您为什么决定这样做,而不是像 @mxii 建议的那样将 myObject 放在组件之前?
      • 我在@mxii 发布答案之前这样做了。我不知道正确的答案是什么,但这对我有用。
      猜你喜欢
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2017-10-03
      • 2018-11-10
      • 2021-08-15
      相关资源
      最近更新 更多