简体   繁体   English

Angular 12 - 无法读取未定义的属性(读取“值”)

[英]Angular 12 - Cannot read properties of undefined (reading 'values')

I am in the process of upgading Angular Version from 8 to 12. I have succesfully upgraded from 8 to 11. But After upgrading the angular Version to 12, i got a error "loadComponent TypeError: Cannot read properties of undefined (reading 'values')" We have used the componentFactoryResolver to load the component dynamically.我正在将 Angular 版本从 8 升级到 12。我已成功从 8 升级到 11。但是在将 angular 版本升级到 12 后,我收到错误“loadComponent TypeError: Cannot read properties of undefined (reading 'values )"我们使用了componentFactoryResolver来动态加载组件。 May i need to change any of configuration in my file.我是否需要更改文件中的任何配置。

I have attched my code here as well:我也在这里附加了我的代码:

   import { Component, ComponentFactoryResolver, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TemplateWithMainHeaderDirective } from './template-with-mainheader.directive';

@Component({
  selector: 'app-template-with-mainheader',
  templateUrl: './template-with-mainheader.component.html',
  styleUrls: ['./template-with-mainheader.component.scss']
})
/** template-content-area component*/
export class TemplateWithMainHeaderComponent {

  @ViewChild(TemplateWithMainHeaderDirective, { static: true }) templatedirectiveHost: TemplateWithMainHeaderDirective;

  currentExerciseRef: any;
  currentComponentRef: any;
  backurl: string = "/";

  /** template-content-area ctor */
  constructor(private componentFactoryResolver: ComponentFactoryResolver, private activatedRoute: ActivatedRoute) {
  }

  ngOnInit(): void {
    var initcomponent = this.activatedRoute.snapshot.data['initselector'];
    this.loadComponent(initcomponent);
  }

  loadComponent(exerciseRef) {
    try {
      if (this.currentExerciseRef) {
        this.currentExerciseRef.destroy();
      }
      const factories = Array.from(this.componentFactoryResolver['_factories'].values());
      const factory: any = factories.find((x: any) => x.selector === exerciseRef);
      if (factory) {
        const viewContainerRef = this.templatedirectiveHost.viewContainerRef;
        const componentRef = viewContainerRef.createComponent(factory);
        this.currentExerciseRef = componentRef;
      }
    } catch (e) {
      console.log("loadComponent", e);
    }
  }

========================== ===========================

Error is:错误是:

loadComponent TypeError: Cannot read properties of undefined (reading 'values')
    at TemplateWithMainHeaderComponent.loadComponent (template-with-mainheader.component.ts:33:80)
    at TemplateWithMainHeaderComponent.ngOnInit (template-with-mainheader.component.ts:25:10)
    at callHook (core.js:2538:1)
    at callHooks (core.js:2507:1)
    at executeInitAndCheckHooks (core.js:2458:1)
    at refreshView (core.js:9499:1)
    at refreshEmbeddedViews (core.js:10609:1)
    at refreshView (core.js:9508:1)
    at refreshComponent (core.js:10655:1)
    at refreshChildComponents (core.js:9280:1)

Can anyone helpme to fix this issue.谁能帮我解决这个问题。

if i see your code, i notice that you want to access to a child component and use the componentFactoryResolver to create a dynamic component, but you are not using the right lifecycle hook, when you use the OnInit lifecycle hook, the child component is not yet loaded, so you need to use the ngAfterViewInit lifecycle hook, that allows you to use a fully loaded components and prevent this kind of errors.如果我看到你的代码,我注意到你想访问一个子组件并使用 componentFactoryResolver 创建一个动态组件,但是你没有使用正确的生命周期钩子,当你使用 OnInit 生命周期钩子时,子组件不是尚未加载,因此您需要使用ngAfterViewInit生命周期钩子,它允许您使用完全加载的组件并防止此类错误。 check this similar stackoverflow issue too.检查这个类似的stackoverflow问题
and i advice you to debug step by step, that means, you console log the parent object, then the property etc... to know exactly where is the problem.我建议您逐步调试,也就是说,您控制台记录父 object,然后是属性等...以确切知道问题出在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 12:错误类型错误:无法读取未定义的属性(读取“nativeElement”) - Angular 12: ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement') 错误:无法读取未定义的属性(读取“管道”)Angular12 - Error: Cannot read properties of undefined (reading 'pipe') Angular12 获取 TypeError:在发送 ajax 请求(Angular 12)时无法读取未定义的属性(读取“管道”) - Getting TypeError: Cannot read properties of undefined (reading 'pipe') while sending ajax request (Angular 12) Angular:Cannot read properties of undefined (reading '0') - Angular:Cannot read properties of undefined (reading '0') 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then') 无法读取未定义的属性(读取“4”) - Cannot read properties of undefined (reading '4') 无法读取未定义的属性(读取“______”) - Cannot read properties of undefined (reading '______') 无法读取未定义的属性(读取“打开”) - Cannot read properties of undefined (reading 'on') Angular 7:TypeError:无法读取未定义的属性(读取“substr”) - Angular 7: TypeError: Cannot read properties of undefined (reading 'substr') ProductScreen.js:12 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'params') - ProductScreen.js:12 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'params')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM