2017-10-19 59 views
0

我收到错误:楼盘X上不存在类型Y []

questions does not exist on type 'Quiz[]'

这是我的模型

export interface Quiz { 
$key ?: string; 
categorie ?: string; 

questions : Questions[] 
} 

export interface Questions { 

id ?: number; 
question ?: string; 
answer?: string; 

} 

这是我的代码

import {Quiz} from "../models/quiz"; 

export class ProfComponent { 

quiz = [] as Quiz[]; 


nextQuestion(quiz) { 

    this.quiz.questions.push(quiz.questions) 
} 


} 

我不能将数据推入quiz.questions

什么可能是错的?

+0

异步nextQuestion(测验){ 等待this.quiz.push(测验) } – Afnor

回答

0

您通过Quiz[]声明quiz属性作为数组,因此,你需要访问数组的项目能够访问questions财产。

但你可能想要的属性重新声明只是Quiz ...

而且你需要先初始化questions阵列能够调用push就可以了。

import {Quiz} from "../models/quiz"; 

export class ProfComponent { 

    quiz = {questions: []} as Quiz; 

    nextQuestion(quiz) { 
    this.quiz.questions.push(quiz.questions) 
    } 

} 

或者,如果你想拥有测验的阵列,像这样做:

import {Quiz} from "../models/quiz"; 

export class ProfComponent { 

    quizes = [{questions: []}] as Quiz[]; // array of Quiz objects 

    nextQuestion(quiz) { 
    this.quizes[0].questions.push(quiz.questions); // access the first quiz 
    } 

} 
+0

但是,如果我想**测验**变量是一个数组我怎么能这样做? – Afnor

+0

然后你需要在该数组中有一些测验实例。将更新我的答案。 –

0

前推值,则需要初始化数组,

nextQuestion(quiz) { 
    this.quiz.questions = []; 
    this.quiz.questions.push(quiz.questions); 
} 
+0

同样的错误在你添加的行 – Afnor

0

要么

quiz = new Quiz(); 

this.quiz[0].questions.push(quiz.questions) 

当初始化quiz为数组,你需要解决一个特定的阵列项获得实际Quiz,或者你不把它的数组,如果不是意图

+0

我想存储几个**测验**到测验[],所以我需要这个数组,我试过你的解决方案,但我得到的错误** this.quiz [0] .questions.push不是一个函数** – Afnor

+0

有需要在'this.quiz [0]'中首先是一个'Quiz'。也许是'this.quiz.push(new Quiz());''this.quiz [0] .questions.push(quiz.questions)之前'' –

+0

我忘记提及我已经成功推出第一个我的数组中的数据,但我无法访问变量** this.quiz.questions **推出更多的问题,我不知道为什么! – Afnor

相关问题