typescript 模块“AppModule”导入的意外值“[object Object]”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43078541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unexpected value '[object Object]' imported by the module 'AppModule'
提问by Kaan Bursa
I want to import Pipe Module to my app,
我想将管道模块导入我的应用程序,
import { NgModule } from '@angular/core';
import { PipeTransform, Pipe } from '@angular/core';
@Pipe({ name: 'values', pure: false })
export class PipeModule implements PipeTransform {
transform(value: any, args: any[] = null): any {
return Object.keys(value).map(key => value[key]);
}
static forRoot() {
return {
ngModule: PipeModule,
providers: [],
};
}
}
this is my pipe module
这是我的管道模块
at my app.module.ts
在我的 app.module.ts
imports: [
...,
PipeModule.forRoot()
I import it like this and I get this error
我像这样导入它,但出现此错误
Unexpected value '[object Object]' imported by the module 'AppModule
回答by Francesco Borzi
I got the same error (which is very generic), but for a totally different reason.
我遇到了同样的错误(这是非常通用的),但出于完全不同的原因。
My solution won't solve the original question, but I write it here anyway in case someone has the same problem:
我的解决方案不会解决原来的问题,但我还是写在这里以防万一有人遇到同样的问题:
In my unit tests, I was accidentally declaring a provider
object inside the imports
array instead of the providers
one. That was causing the problem.
在我的单元测试中,我不小心provider
在imports
数组中声明了一个对象而不是一个对象providers
。那是造成问题的原因。
回答by snorkpete
Your class PipeModule
is not decorated with the @NgModule
decorator - so it's not a module. Hence it can't be imported into your AppModule
- only @NgModules
are allowed to be imported.
你的类PipeModule
没有用@NgModule
装饰器装饰 - 所以它不是一个模块。因此它不能导入到您的AppModule
- 只@NgModules
允许导入。
Based on what you're doing, your first code example is just creating a standard Angular pipe so please change the name of that class to something like ValuesPipe
so it's not misleading.
根据您在做什么,您的第一个代码示例只是创建一个标准的 Angular 管道,因此请将该类的名称更改为类似的名称,ValuesPipe
以免误导。
At that point, you have two options: you can declare your new ValuesPipe
in AppModule
or you can create a whole new NgModule
to store your new ValuesPipe
and then import that new NgModule
into your AppModule
.
此时,您有两个选择:您可以在其中声明新的ValuesPipe
,AppModule
或者您可以创建一个全新的NgModule
来存储您的新ValuesPipe
,然后将该新NgModule
导入到您的AppModule
.
Please note that in both options below, I've assumed that you've renamed the class in your first example to ValuesPipe
.
请注意,在下面的两个选项中,我假设您已将第一个示例中的类重命名为ValuesPipe
.
so, Option 1:
因此,选项 1:
in app.module.ts,
在app.module.ts 中,
declarations: [
.....,
ValuesPipe,
]
Or Option 2:
或选项2:
create a new class PipeModulelooking like:
创建一个新的PipeModule类,如下所示:
@NgModule({
imports: CommonModule, (from @angular/core)
declarations: ValuesPipe,
})
export class PipeModule { }
And then, in app.module.ts,
然后,在app.module.ts 中,
imports: [
......,
PipeModule
]