我的html代码是
profile.component.html
<input style="display: none" type="file" (change)="onFileChanged($event)" #fileInput>
<button (click)="fileInput.click()">Select File</button>
profile.component.ts
selectedFile: File;
onFileChanged(event) {
this.selectedFile = event.target.files[0]
}
但是当我单击按钮时,出现此错误
ProfileComponent.html:14 ERROR TypeError: Cannot read property 'click' of undefined
at Object.eval [as handleEvent] (ProfileComponent.html:14)
at handleEvent (core.js:10251)
at callWithDebugContext (core.js:11344)
at Object.debugHandleEvent [as handleEvent] (core.js:11047)
at dispatchEvent (core.js:7710)
at core.js:8154
at HTMLButtonElement.<anonymous> (platform-browser.js:988)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3811)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
好心的人救了我一天。
注意:我在用户模块中使用延迟加载