将“model”分配给数组时出错

时间:2018-04-18 09:55:27

标签: angular

在我的列表组件中,我得到一个model并分配给数组。但是得到一个错误:

Type '{ setup: string; punchline: string; hide: true; }' is not assignable to type 'Joke'.
Property 'toggle' is missing in type '{ setup: string; punchline: string; hide: true; }'.

这里出了点问题。任何人帮我解决这个问题?

这是我的模特:(笑话。)

export class Joke {
    public setup:string;
    public punchline:string;
    public hide:boolean;

    constructor(setup:string,punchline:string){
        this.setup = setup;
        this.punchline = punchline;
        this.hide = true;
    }

    toggle(){
        this.hide = !this.hide;
    }
}

这是我的组件:(我在this.jokes处收到错误)

import { Component, OnInit } from '@angular/core';
import { Joke } from '../domain-models/joke';

@Component({
    selector: 'joke-list',
    templateUrl: './joke-list.component.html',
    styleUrls: ['./joke-list.component.css']
})
export class JokeListComponent {

    jokes:Joke[];

    constructor(){
        this.jokes = [
            {
                setup: "What did the cheese say when it looked in the mirror?",
                punchline: "Hello-Me (Halloumi)",
                hide:true
            },
            {
                setup: "What kind of cheese do you use to disguise a small horse?",
                punchline: "Mask-a-pony (Mascarpone)",
                hide:false
            },
            {
                setup: "A kid threw a lump of cheddar at me",
                punchline: "I thought ‘That’s not very mature’",
                hide:true
            }
        ];
    }

}

这里有什么问题?有人纠正我吗?

2 个答案:

答案 0 :(得分:2)

使用课程时使用char* receive_string()运算符。像这样。

char string[55]

答案 1 :(得分:1)

使用此,

<强> Joke.ts

export class Joke {
    public setup:string;
    public punchline:string;
    public hide:boolean;

    constructor(setup:string,punchline:string, hide:boolean = true){
        this.setup = setup;
        this.punchline = punchline;
        this.hide = hide;
    }

    toggle(){
        this.hide = !this.hide;
    }
}

<强>组件

构造(){

this.jokes = [
        new Joke(
             "What did the cheese say when it looked in the mirror?",
             "Hello-Me (Halloumi)"
        ),
         new Joke(
            "What kind of cheese do you use to disguise a small horse?",
             "Mask-a-pony (Mascarpone)",
            false
         ),
        new Joke(
            "A kid threw a lump of cheddar at me",
            "I thought ‘That’s not very mature’"
        )
    ];
}