javascript - When importing objects from other modules in angularjs2, the compilation prompt attribute does not exist in the object
给我你的怀抱
给我你的怀抱 2017-05-18 10:57:37
0
1
568

In angularjs2, import objects from other modules (another object is nested within the object) and use it. When compiling, the console prompts that the object does not contain the nested object, so the compilation fails. However, if another module is included in The object declaration is copied directly to the same file, and everything works fine.

The error prompted by the console is as follows:

The problem code is as follows:

//exercise-detail.component.ts
import { Component } from '@angular/core';

import { Exercise } from './exercise';


let exercise: Exercise = {
    id: 1,
    name: 'bench press',
    exerciseSets: [{
        id: 1,
        num: 1,
        previousWeight: 0,
        weight: 10,
        reps: 12,
        isFinish: true
    },{
        id: 2,
        num: 2,
        previousWeight: 0,
        weight: 10,
        reps: 12,
        isFinish: false
    }]
};

@Component({
    selector: 'exercise-detail',
    template: `
       <table border='1'>
           <tr>
               <td>finish?</td>
               <td>{{exercise.exerciseSets[1].isFinish}}</td>
           </tr>
       </table>
    `
})
export class ExerciseDetailComponent {

    exercise = exercise;
}
    
//exercise.ts
export class ExerciseSet {
    id: number;
    num: number;
    previousWeight: number;
    weight: number;
    reps: number;
    isFinish: boolean;
}

export class Exercise {

    id: number;
    name: string;
    exerciseSets: ExerciseSet[];
}

When the code in the two files is merged into one file, it can be compiled and the output will be correct.
The merged code is as follows:

//exercise-detail.component.ts
import { Component } from '@angular/core';

export class ExerciseSet {
    id: number;
    num: number;
    previousWeight: number;
    weight: number;
    reps: number;
    isFinish: boolean;
}

export class Exercise {

    id: number;
    name: string;
    exerciseSets: ExerciseSet[];
}

let exercise: Exercise = {
    id: 1,
    name: 'bench press',
    exerciseSets: [{
        id: 1,
        num: 1,
        previousWeight: 0,
        weight: 10,
        reps: 12,
        isFinish: true
    },{
        id: 2,
        num: 2,
        previousWeight: 0,
        weight: 10,
        reps: 12,
        isFinish: false
    }]
};

@Component({
    selector: 'exercise-detail',
    template: `
       <table border='1'>
           <tr>
               <td>finish?</td>
               <td>{{exercise.exerciseSets[1].isFinish}}</td>
           </tr>
       </table>
    `
})
export class ExerciseDetailComponent {

    exercise = exercise;
}

I believe this problem is easy to solve. It is not a grammatical or logical error. It should be that Typescript has requirements for imported files. I have just used Typescript for a short time and am not familiar with it. I hope you can give me some advice.

给我你的怀抱
给我你的怀抱

reply all(1)
伊谢尔伦

I have also written something like this, but when I assigned the value, I did a new in the constrctor and it was ok

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template