【问题标题】:FIXED-IONIC 3: property 'questions' does not exist on type 'Object'FIXED-IONIC 3:“对象”类型上不存在属性“问题”
【发布时间】:2019-02-18 11:34:27
【问题描述】:

我遇到了关于这 1 行的错误,我尝试了不同的资源来修复它,但不幸的是我不能。有什么帮助吗?我正在做一个简单的测验,不介意 atm 提供的数据。

这是我的 data.ts。这是我遇到麻烦的地方。

import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { HttpClient } from '@angular/common/http';

/*
  Generated class for the DataProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class DataProvider {


  data: any;

  constructor(public http: HttpClient) {

  }

  load(){

    if(this.data){
        return Promise.resolve(this.data);
    }

    return new Promise(resolve => {

        this.http.get('assets/data/questions.json').subscribe(data => {
            this.data = data.questions; <----------THIS IS LINE IS DISPLAYING ERROR
            resolve(this.data);
        });

    });

  }

}

这是我的名为 questions.json 的 json 文件

{
    "questions": [

        {
            "flashCardFront": "<img src='assets/questionimg/12_plate1.gif' />",
            "flashCardBack": "Helicopter",
            "flashCardFlipped": false,
            "questionText": "What is this?",
            "answers": [
                {"answer": "Helicopter", "correct": true, "selected": false},
                {"answer": "Plane", "correct": false, "selected": false},
                {"answer": "Truck", "correct": false, "selected": false}
            ]
        },
        {
            "flashCardFront": "<img src='assets/questionimg/8_plate2.gif' />",
            "flashCardBack": "Plane",
            "flashCardFlipped": false,
            "questionText": "What is this?",
            "answers": [
                {"answer": "Helicopter", "correct": false, "selected": false},
                {"answer": "Plane", "correct": true, "selected": false},
                {"answer": "Truck", "correct": false, "selected": false}
            ]
        },
        {
            "flashCardFront": "<img src='assets/questionimg/29_plate3.gif' />",
            "flashCardBack": "Truck",
            "flashCardFlipped": false,
            "questionText": "What is this?",
            "answers": [
                {"answer": "Helicopter", "correct": false, "selected": false},
                {"answer": "Plane", "correct": false, "selected": false},
                {"answer": "Truck", "correct": true, "selected": false}
            ]
        }

    ]
}

我的 TypeScript 文件是我生成测验的地方。

import { Component, ViewChild} from '@angular/core';
import { NavController} from 'ionic-angular';
import { DataProvider } from '../../providers/data/data';

/**
 * Generated class for the IshiharaQuestionsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-ishihara-questions',
  templateUrl: 'ishihara-questions.html',
})

export class IshiharaQuestionsPage {

  @ViewChild('slides') slides: any;

    hasAnswered: boolean = false;
    score: number = 0;

    slideOptions: any;
    questions: any;

  constructor(public navCtrl: NavController, public dataService: DataProvider) {
  }

  ionViewDidLoad() {

    this.slides.lockSwipes(true);

    this.dataService.load().then((data) => {

        data.map((question) => {

            let originalOrder = question.answers;
            question.answers = this.randomizeAnswers(originalOrder);
            return question;

        });     

        this.questions = data;

    });

  }

  nextSlide(){
    this.slides.lockSwipes(false);
    this.slides.slideNext();
    this.slides.lockSwipes(true);
  }

  selectAnswer(answer, question){

    this.hasAnswered = true;
    answer.selected = true;
    question.flashCardFlipped = true;

    if(answer.correct){
        this.score++;
    }

    setTimeout(() => {
        this.hasAnswered = false;
        this.nextSlide();
        answer.selected = false;
        question.flashCardFlipped = false;
    }, 3000);
  }


  randomizeAnswers(rawAnswers: any[]): any[] {

    for (let i = rawAnswers.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        let temp = rawAnswers[i];
        rawAnswers[i] = rawAnswers[j];
        rawAnswers[j] = temp;
    }

    return rawAnswers;

  }

  restartQuiz() {
    this.score = 0;
    this.slides.lockSwipes(false);
    this.slides.slideTo(1, 1000);
    this.slides.lockSwipes(true);
  }
}

最后是我展示测验的 ion-html 文件。

<ion-content>

  <ion-slides #slides>

      <ion-slide class="start-slide">
          <button ion-button color="primary" (click)="nextSlide()">Start!</button>
      </ion-slide>

      <ion-slide *ngFor="let question of questions; let i = index;">

          <h3>Question {{i+1}}</h3>

          <flash-card [isFlipped]="question.flashCardFlipped">
              <div class="flash-card-front" [innerHTML]="question.flashCardFront"></div>
              <div class="flash-card-back" [innerHTML]="question.flashCardBack"></div>
          </flash-card>

          <h3>{{question.questionText}}</h3>

          <ion-list no-lines radio-group>

              <ion-item *ngFor="let answer of question.answers; let i = index;">

                  <ion-label>{{i+1}}. {{answer.answer}}</ion-label>
                  <ion-radio (click)="selectAnswer(answer, question)" [checked]="answer.selected" [disabled]="hasAnswered"></ion-radio>

              </ion-item>

          </ion-list>

      </ion-slide>

      <ion-slide>
          <h2>Final Score: {{score}}</h2>

          <button (click)="restartQuiz()" ion-button full color="primary">Start Again</button>

      </ion-slide>

  </ion-slides>

</ion-content>

【问题讨论】:

  • 您遇到的错误是什么?
  • 这个问题已经解决了。但非常感谢您的回复

标签: angular ionic-framework ionic3


【解决方案1】:

需要将response改为any的类型如下,

this.http.get('assets/data/questions.json').subscribe((data:any) => {

【讨论】:

  • 感谢您的快速响应,陛下。这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 2019-10-11
  • 2018-02-10
  • 2018-03-03
相关资源
最近更新 更多