我尝试创建一个示例应用程序,我希望在父组件的按钮单击事件上动态加载组件。但是,当我单击按钮时,组件未加载,而是在浏览器控制台中收到以下错误。请访问plunker申请代码
zone.js:461Unhandled Promise rejection: Cannot read property 'createComponent' of undefined ; Zone: angular ; Task: Promise.then ; Value: TypeError: Cannot read property 'createComponent' of undefined
at eval (https://run.plnkr.co/EQ3xbfuUPWfMTU2C/src/app.ts!transpiled:38:45)
at ZoneDelegate.invoke (https://npmcdn.com/zone.js@0.6.12/dist/zone.js:323:29)
at Object.onInvoke (https://npmcdn.com/@angular/core/bundles/core.umd.js:9100:45)
at ZoneDelegate.invoke (https://npmcdn.com/zone.js@0.6.12/dist/zone.js:322:35)
at Zone.run (https://npmcdn.com/zone.js@0.6.12/dist/zone.js:216:44)
at https://npmcdn.com/zone.js@0.6.12/dist/zone.js:571:58
at ZoneDelegate.invokeTask (https://npmcdn.com/zone.js@0.6.12/dist/zone.js:356:38)
at Object.onInvokeTask (https://npmcdn.com/@angular/core/bundles/core.umd.js:9091:45)
at ZoneDelegate.invokeTask (https://npmcdn.com/zone.js@0.6.12/dist/zone.js:355:43)
at Zone.runTask (https://npmcdn.com/zone.js@0.6.12/dist/zone.js:256:48)
答案 0 :(得分:1)
如果查询@ViewChild(Loader, ...)
,则需要确保它已实例化。为此,您需要将Loader
添加到directives: [...]
directives: [Loader]
})
export class App {