Javascript 在component.ts中存储嵌套数组
Angular在我尝试嵌套数组时为我的代码抛出一个错误。有人能帮我吗?我的代码如下所示:Javascript 在component.ts中存储嵌套数组,javascript,arrays,angular,typescript,components,Javascript,Arrays,Angular,Typescript,Components,Angular在我尝试嵌套数组时为我的代码抛出一个错误。有人能帮我吗?我的代码如下所示: parents = { pfirst_name: '', pmiddle_name: '', plast_name: '', ... } students = { sfirst_name: '', smiddle_name: '', slast_name: '', ... } both = { this.parents,
parents = {
pfirst_name: '',
pmiddle_name: '',
plast_name: '',
...
}
students = {
sfirst_name: '',
smiddle_name: '',
slast_name: '',
...
}
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
Module parse failed: Unexpected token (61:18)
You may need an appropriate loader to handle this file type.
| };
| this.both = {
| this: .parents,
| this: .students
| };
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'.
我还尝试了另一种方式:
both = {
parents = {
pfirst_name: '',
pmiddle_name: '',
plast_name: '',
...
},
students = {
sfirst_name: '',
smiddle_name: '',
slast_name: '',
...
}
}
我得到这个错误:
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
| };
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
};
在第二种方法中,必须使用:而不是=,如下所示:
parents = {
pfirst_name: '',
pmiddle_name: '',
plast_name: '',
...
}
students = {
sfirst_name: '',
smiddle_name: '',
slast_name: '',
...
}
both = {
this.parents,
this.students
}
both = {
parents: {
pfirst_name: '',
pmiddle_name: '',
plast_name: '',
...
},
students: {
sfirst_name: '',
smiddle_name: '',
slast_name: '',
...
}
}
这是一个嵌套对象。语法是{outer:{inner:1}}
,注意对象文本中没有等号。