gpt4 book ai didi

angular - 如何解决使用 Angular 4 检测到的循环依赖?

转载 作者:太空狗 更新时间:2023-10-29 18:30:13 26 4
gpt4 key购买 nike

我正在使用 Angular 4 简单表单,当我使用命令行编译项目时出现错误,如何解决警告。参见错误图像 1 : /image/N1AH4.png

>     WARNING in Circular dependency detected:
> src\app\shopinfo\shopinfo.component.ts -> src\app\shopinfo\shopinfo.module.ts ->
> src\app\shopinfo\shopinfo.routing.ts ->
> src\app\shopinfo\shopinfo.component.ts

组件.ts: 下面是我的component.ts,这里声明component.html values的form values

  [import {Component, OnInit, ViewEncapsulation, ElementRef, OnDestroy} from '@angular/core';
import {FormGroup, FormControl, Validators} from "@angular/forms";
import {IOption} from "ng-select";
import {ShopInfo} from './shopinfo.module';
@Component({
selector: 'app-shopinfo',
templateUrl: './shopinfo.component.html',
styleUrls: \['./shopinfo.component.css'\]
})
export class shopinfoComponent implements OnInit {
myForm: FormGroup;
shopinformation: any;
submitted: boolean;
constructor(private shopinfo: ShopInfo) {
let shopname = new FormControl('', Validators.required);
let ownername = new FormControl('', Validators.required);
let license = new FormControl('', Validators.required);
let dlNumber = new FormControl('', Validators.required);
let gst = new FormControl('', Validators.required);
this.myForm = new FormGroup({
shopname: shopname,
ownername: ownername,
license: license,
dlNumber: dlNumber,
gst: gst
});
}
ngOnInit() {
}
onSubmit() {
this.submitted = true;
this.createRecord();
}
private createRecord(): void {
this.shopinfo.createShop(this.shopinformation);
}
}][1]

模块.ts: 下面一个是我的module.ts

     import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule} from "@angular/router";
import {SharedModule} from "../shared/shared.module";
import {shopinfoRoutes} from './shopinfo.routing';
import {shopinfoComponent} from './shopinfo.component';

@NgModule({
imports: [
CommonModule,
RouterModule.forChild(shopinfoRoutes),
SharedModule,
],
declarations: [shopinfoComponent]
})

export class ShopInfo {}


[1]: /image/N1AH4.png

组件.服务.ts:

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';

@Injectable()
export class shopService {
handleError: any;
headers: any;
private shopUrl = 'api/createRecord';
private header = new Headers({'Content-Type': 'application/json'});

constructor(private http: Http) {}

createShop(shopcreate: any): Promise<any> {
return this.http
.post(this.shopUrl, JSON.stringify(shopcreate), {headers: this.headers})
.toPromise()
.then(res => res.json() as any)
.catch(this.handleError);
}
}

image1

最佳答案

如果你看一下,它是非常不言自明的:

src\app\shopinfo\shopinfo.component.ts 
-> src\app\shopinfo\shopinfo.module.ts
-> src\app\shopinfo\shopinfo.routing.ts
-> src\app\shopinfo\shopinfo.component.ts

这意味着

  • shopinfo组件导入shopinfo模块
  • shopinfo模块导入shopinfo路由
  • shopinfo路由导入shopinfo组件
  • 并且由于 shopinfo 组件导入了该模块,它再次启动。

这是一个循环依赖:您不能离开您创建的导入循环。

现在您已经记住了这一点,您只需删除一个您不使用的导入。

关于angular - 如何解决使用 Angular 4 检测到的循环依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47881785/

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