当我尝试嵌套数组时,Angular 为我的代码抛出错误。有人可以帮我吗?我的代码如下所示:
parents = {
pfirst_name: '',
pmiddle_name: '',
plast_name: '',
...
}
students = {
sfirst_name: '',
smiddle_name: '',
slast_name: '',
...
}
both = {
this.parents,
this.students
}
我得到这个错误:
ERROR in ./src/app/app.component.ts
Module parse failed: Unexpected token (61:18)
You may need an appropriate loader to handle this file type.
| };
| this.both = {
| this: .parents,
| this: .students
| };
我也尝试了一种不同的方式:
both = {
parents = {
pfirst_name: '',
pmiddle_name: '',
plast_name: '',
...
},
students = {
sfirst_name: '',
smiddle_name: '',
slast_name: '',
...
}
}
我收到此错误:
ERROR in src/app/app.component.ts(18,9): error TS2552: Cannot find name 'parents'. Did you mean 'parent'?
src/app/app.component.ts(18,17): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
src/app/app.component.ts(28,9): error TS2304: Cannot find name 'students'.
任何帮助深表感谢!!
您在第一种方法中使用的是对象,而不是数组。试试这个:
// Array
both = [
this.parents,
this.students
];
// Or this alternatively
both = {
parents: this.parents,
students: this.students
};
在你的第二个方法中,你必须使用 : 而不是 =,像这样:
both = {
parents: {
pfirst_name: '',
pmiddle_name: '',
plast_name: '',
...
},
students: {
sfirst_name: '',
smiddle_name: '',
slast_name: '',
...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句