gpt4 book ai didi

angular - 将模块导入到另一个模块中以访问服务

转载 作者:行者123 更新时间:2023-12-02 18:26:28 25 4
gpt4 key购买 nike

我有以下模块:

import { NgModule } from '@angular/core';
import { ActivitiesService } from '../services/activities.service';

@NgModule({
declarations: [],
imports: [ActivitiesService],
providers: [],
bootstrap: [],
exports: [ActivitiesService]
})

export class ActivitiesModule { }

ActivitiesService 将通过我的应用程序中的不同组件使用,因此我决定为 ActivitiesService 创建一个共享模块,该模块导入服务并导出它以使其可用用于导入其他模块。

然后我创建了一个新的模块,该模块将导入上面的模块,如下所示:

import { NgModule } from '@angular/core';

// Components
import { ProfileComponent } from '../component/profile/profile.component';

// Routing
import { profileRouting } from '../routing/profile-routing';

// Shared << IMPORTED OTHER MODULE HERE
import { ActivitiesModule } from '../../shared/modules/activities.module';


import { FormsModule, ReactiveFormsModule } from '@angular/forms';

// Authentication Guard
import { AuthenticationGuard } from '../../shared/guards/authentication.guard';


@NgModule({
imports: [
profileRouting,
FormsModule,
ReactiveFormsModule,
ActivitiesModule],
declarations: [
ProfileComponent],
providers: [AuthenticationGuard]
})

export class ProfileModule { }

现在在我的 ProfileComponent 中我有以下声明:

// Services
import { ActivitiesService } from '../../../shared/services/activities.service';

ProfileComponent的构造函数

constructor(private activityService: ActivitiesService) {}

我的 ngOnInit 方法如下所示:

ngOnInit() {

this.activityService.getActivities();

}

但是,当我运行此命令并导航到 Profile 页面时,我收到错误消息:

Error: Uncaught (in promise): Error: Unexpected value 'ActivitiesService' imported by the module 'ActivitiesModule'. Please add a @NgModule annotation. Error: Unexpected value 'ActivitiesService' imported by the module 'ActivitiesModule'. Please add a @NgModule annotation.

我已经检查了命名约定,检查了 @NgModule 注释,一切都是正确的。有人可以阐明我在这里做错了什么吗?

最佳答案

通过执行以下操作解决:

事件模块:

import { NgModule } from '@angular/core';
import { ActivitiesService } from '../services/activities.service';

@NgModule({
declarations: [],
imports: [],
providers: [ActivitiesService],
bootstrap: [],
exports: []
})

export class ActivitiesModule { }

导入到 ProfileModule 中保持不变。

关于angular - 将模块导入到另一个模块中以访问服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com