Angular 在 component.ts 中存储嵌套数组

伊森·汤姆

当我尝试嵌套数组时,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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我在Angular 2中需要app.component.ts吗?

Angular Component.ts 模块中的对象类型问题

login.component.html 中的 Angular 表单不向 login.component.ts 发送数据

来自 app.component.ts 的 Angular 路由

Angular ngx-editor 从其他 components.ts 文件中的 app.component.ts 外包配置

Angular2 将存储在变量中的数据从 app.component.ts(父组件)传送到其他组件

我需要在 angular4 中关闭 component.ts 中的弹出消息

在 Angular 中过滤嵌套数组

Angular 2调试器说一个元素未知。在.component.ts文件中

Angular - 为什么表单中的数据不传递给 component.ts

从 observable 中获取价值以在 component.ts 中使用它 - Angular

如何使用.ts文件中的嵌套数组

如何在 angular 的 component.ts 之外使用 datepipe?

在component.ts中找不到名称“ $”

Angular 6 - 如何使用 *ngFor 从表中动态创建的输入框中获取 component.ts 中的输入值?

Angular Material MatTableModule - 如何从模板中的 mat-table 外部访问 component.ts 文件中返回的数据?

Angular 8:comonent.html 中使用的值不是 component.ts(azure 应用服务)中的值

Angular-当它们在component.ts文件中启动时,如何从指令内部检测表单事件?

如何在HTML中采用输入类型Date并在angular7 component.ts文件中使用?

在 Angular 7 中获取错误 ./src/app/app.component.ts 模块未找到:错误:无法解析

使用angular / ts在component.ts中进行数学运算,但是+无效,但是-

Angular 5 嵌套数组

嵌套数组的 Angular PatchValue

在JS / Angular中过滤嵌套数组无法正常工作

如何在Angular JS中打印嵌套数组

对象Angular 2+ ngrx中的嵌套数组

如何在Angular中遍历嵌套数组

Angular 7将变量从component传递到app.component.ts

Angular中的@Directive与@Component