Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度组件未从解析初始化_Angular_Jhipster - Fatal编程技术网

Angular 角度组件未从解析初始化

Angular 角度组件未从解析初始化,angular,jhipster,Angular,Jhipster,我有一个JHipster应用程序(Angular 5+Spring Boot 2),我正在尝试为我已经创建的模型对象创建一个新路由(不是使用实体生成器创建的,它只是一个简单的模型)。问题是,即使我可以看到Angular应用程序正在调用正确的API并获得有效的JSON响应,我的组件也没有初始化 以下是模型(简化): 以及加载讲师对象的服务: type EntityResponseType = HttpResponse<IInstructor>; @Injectable({ provid

我有一个JHipster应用程序(Angular 5+Spring Boot 2),我正在尝试为我已经创建的模型对象创建一个新路由(不是使用实体生成器创建的,它只是一个简单的模型)。问题是,即使我可以看到Angular应用程序正在调用正确的API并获得有效的JSON响应,我的组件也没有初始化

以下是模型(简化):

以及加载
讲师
对象的服务:

type EntityResponseType = HttpResponse<IInstructor>;
@Injectable({ providedIn: 'root' })
export class InstructorService {
    public resourceUrl = SERVER_API_URL + 'api/instructor';
    constructor(private http: HttpClient) {}

    get(name: string): Observable<EntityResponseType> {
        return this.http.get<IInstructor>(`${this.resourceUrl}/${name}`, { observe: 'response' });
    }
}
这是我在应用程序其他部分所遵循的模式(基本上是从自动生成的JHipster代码中重复使用的),它工作得很好,所以我不确定我遗漏了什么

更新

正如我所怀疑的,这是人为错误。我从另一个组件复制了路线,并且没有更改“解决”值(它使用的是骑乘而不是讲师)。非常感谢您的建议

resolve: {
    instructor: InstructorResolve
},

我想下面做一个小的修正可以得到组件中所需的数据

const routes: Routes = [
{ path: 'instructor/:name',
    component: InstructorComponent,
    resolve: {
        ride: InstructorResolve
    },
    data: {
        pageTitle: 'Instructor Details'
    }
}]
在组件中,进行如下更改

export class InstructorComponent implements OnInit {
  service: InstructorService;
  instructor: IInstructor;

  constructor(private instructorService: InstructorService,
              private activatedRoute: ActivatedRoute,
              private router: Router) {
      this.instructor = this.route.snapshot.data["ride"];
      this.service = instructorService;
  }

}
您需要使用组件中路由部分用于解析的相同密钥来检索数据,以便从路由中获取解析的数据


我希望这会有所帮助。

我想下面做一个小的更正可以得到组件中所需的数据

const routes: Routes = [
{ path: 'instructor/:name',
    component: InstructorComponent,
    resolve: {
        ride: InstructorResolve
    },
    data: {
        pageTitle: 'Instructor Details'
    }
}]
在组件中,进行如下更改

export class InstructorComponent implements OnInit {
  service: InstructorService;
  instructor: IInstructor;

  constructor(private instructorService: InstructorService,
              private activatedRoute: ActivatedRoute,
              private router: Router) {
      this.instructor = this.route.snapshot.data["ride"];
      this.service = instructorService;
  }

}
您需要使用组件中路由部分用于解析的相同密钥来检索数据,以便从路由中获取解析的数据


我希望这能有所帮助。

尝试使用以下代码

export class InstructorComponent implements OnInit {
  service: InstructorService;
  instructor: IInstructor;

  constructor(private instructorService: InstructorService,
    private activatedRoute: ActivatedRoute,
    private router: Router) {
    this.service = instructorService;
  }

  ngOnInit() {
    this.activatedRoute.snapshot.data("ride").subscribe(({ instructor }) => {
      this.instructor = instructor;
      console.log('Got instructor = ' + instructor);
    });
  }
}

尝试输入以下代码

export class InstructorComponent implements OnInit {
  service: InstructorService;
  instructor: IInstructor;

  constructor(private instructorService: InstructorService,
    private activatedRoute: ActivatedRoute,
    private router: Router) {
    this.service = instructorService;
  }

  ngOnInit() {
    this.activatedRoute.snapshot.data("ride").subscribe(({ instructor }) => {
      this.instructor = instructor;
      console.log('Got instructor = ' + instructor);
    });
  }
}

您是否可以尝试此.activatedRoute.snapshot.data您是否可以尝试此.activatedRoute.snapshot.data将此标记为已接受,因为有关密钥的额外措辞最终导致了问题的出现。谢谢将此标记为已接受,因为关于钥匙的额外赘述最终导致了我的问题。谢谢谢谢,这确实是从另一个组件复制路由而不更改密钥名称的问题。谢谢,这确实是从另一个组件复制路由而不更改密钥名称的问题。
export class InstructorComponent implements OnInit {
  service: InstructorService;
  instructor: IInstructor;

  constructor(private instructorService: InstructorService,
    private activatedRoute: ActivatedRoute,
    private router: Router) {
    this.service = instructorService;
  }

  ngOnInit() {
    this.activatedRoute.snapshot.data("ride").subscribe(({ instructor }) => {
      this.instructor = instructor;
      console.log('Got instructor = ' + instructor);
    });
  }
}