0

将 JSON 字符串作为变量传递时,我无法获取要呈现的表单。如果我将字符串粘贴到 HTML 中,它可以正常工作。很困惑。我的 HTML:

<formio [form]="form" (submit)="onSubmit($event)"></formio>

我预加载 JSON 并从路由数据中提取。我的组件:

  export class FormIOViewerComponent implements OnInit {
  constructor(private route: ActivatedRoute) { }
  form:string='';
  onSubmit(submission: any) {
    console.log(submission); 
  }
  ngOnInit() {    
    this.form=this.route.snapshot.data.Fields;
    console.log(this.form)
  }
}

JSON 字符串在错误之前打印到控制台,我可以再次将从控制台打印的 JSON 字符串复制并粘贴到 HTML 中,它显示正常。在 Chrome 中看到的错误:

form-ioviewer.component.html:1 ERROR TypeError: Cannot read property '1' of null
at new Formio (Formio.js:213)
at Form.setForm (Form.js:185)
at new Form (Form.js:106)
at FormioComponent.push../node_modules/angular-formio/FormioBaseComponent.js.FormioBaseComponent.createRenderer (FormioBaseComponent.js:90)
at FormioComponent.push../node_modules/angular-formio/FormioBaseComponent.js.FormioBaseComponent.setForm (FormioBaseComponent.js:111)
at FormioBaseComponent.js:402
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Zone.run (zone-evergreen.js:124)
at NgZone.runOutsideAngular (core.js:39572)
at FormioComponent.push../node_modules/angular-formio/FormioBaseComponent.js.FormioBaseComponent.ngOnChanges (FormioBaseComponent.js:398)
4

1 回答 1

0

事实证明,除了 JSON 字符串之外,它必须是一个对象。就像是:

export class formIO {
    title:string;
    name:string
    components: string[]
  }

然后将对象传递给表单,而不是字符串。

于 2020-02-07T12:45:00.053 回答