Heim  >  Fragen und Antworten  >  Hauptteil

Nicht erfasster TypeError: this.saveAlpha ist bei der Auswertung keine Funktion

In diesem Code habe ich eine Start- und eine Stopptaste und einen Timer.

Ich möchte Informationen vom Telefon wie DeviceOrientationEvent und Eigenschaften wie absolut, Alpha, Beta und Gamma erhalten, wie in diesem Link gezeigt. Ich weiß nicht, wie ich so etwas machen soll, da ich solche Ereignisse noch nie genutzt habe.

Nachdem ich diese Ereignisse erhalten habe, speichere ich sie z. B. in Richtung[].

<template>
  <q-page padding>
    <div class="text-center text-h5 q-mt-lg">
      {{ $t('tasks.motion.title') }}
    </div>
    <div class="row justify-center q-mt-lg">
      <q-btn
        @click="startTest"
        v-show="!isStarted"
        :label="$t('common.start')"
      />
      <q-btn
        @click="completeTest"
        v-show="isStarted"
        :label="$t('common.complete')"
      />
    </div>
  </q-page>
</template>

<script>
import phone from 'modules/phone'
import userinfo from 'modules/userinfo'
import { format as Qformat } from 'quasar'

const TEST_DURATION = 60

export default {
  name: 'MotionOrientationPage',
  props: {
    sKey: String,
    tId: Number
  },
  data: function () {
    return {
      isSignalCheck: true,
      isStarted: false,
      isCompleted: false,
      timer: undefined,
      totalTime: TEST_DURATION,
      startedTS: undefined,
      completionTS: undefined,
      alpha: []
    }
  },

  mounted: async function () {
     // Events that are running always
     if (window.DeviceMotionEvent) {
       console.log('devicemotion was defined')
        }

     if (window.DeviceOrientationEvent) {
       console.log('GyroScope was defined')
        }
  },

  methods: {
    async startTest () {
      this.isStarted = true
      this.startedTS = new Date()
      this.startTimer()
      phone.screen.forbidSleep()
      if (this.isStarted && this.isCompleted === false) {
       window.addEventListener('deviceorientation', function (event) 
       {
        this.saveAlpha(event.alpha)
        console.log(event.alpha)
       })
      }
    },

     saveAlpha (alpha) {
      this.alpha.push(alpha)
    },

    startTimer () {
      this.totalTime = TEST_DURATION
      this.timer = setInterval(() => this.countDown(), 1000)
    },
    stopTimer () {
      clearInterval(this.timer)
    },

    countDown () {
      if (this.totalTime >= 1) {
        this.totalTime--
      } else {
        this.completeTest()
      }
    },

    completeTest () {
      this.isStarted = false
      this.completionTS = new Date()
      this.stopTimer()
      phone.screen.allowSleep()

      this.isCompleted = true

      // package the report
      const sKey = this.sKey
      const taskId = parseInt(this.taskId)
      const userKey = userinfo.user._key
      let report = {
        userKey: userKey,
        sKey: sKey,
        taskId: taskId,
        createdTS: new Date(),
        startedTS: this.startedTS,
        completionTS: this.completionTS,
        alpha: this.alpha
      }

      this.$router.push({ name: 'reportMotionOrientation', params: { report: report } })
    }
  },

  computed: {
    minutes () {
      return Qformat.pad(Math.floor(this.totalTime / 60))
    },
    seconds () {
      return Qformat.pad(this.totalTime - (this.minutes * 60))
    }
  },

  beforeDestroy: function () {
    this.stopTimer()
    phone.screen.allowSleep()
  }
}
</script>

BEARBEITEN: Wenn ich mithilfe von Code eine Änderung des Alphawerts auf der Registerkarte „Sensor“ in den Entwicklungstools simuliere, erhalte ich Uncaught TypeError: this.saveAlpha is not a function at eval (file1.vue?149e:95) .

P粉930534280P粉930534280205 Tage vor435

Antworte allen(1)Ich werde antworten

  • P粉494151941

    P粉4941519412024-03-28 10:23:27

    我个人没有这样做过,但看起来您需要添加一个侦听器,然后使用一种或多种方法来保存事件中的新数据。

    data() {
      return {
         ...,
         alpha: []
      } 
    },
    mounted: {
      window.addEventListener('deviceorientation', function(event) {
        this.saveAlpha(event.alpha)
      });
      // more event listeners
    },
    methods: {
      saveAlpha(alpha) {
        this.alpha.push(alpha)
      },
      // more saveMethods
    }

    评论更新:

    ...
    data() {
      return {
         continue: "false"
      {
    },
    methods: {
      ...
      listener() {
        if(this.continue) {
          window.addEventListener('deviceorientation', function(event) {
            this.saveAlpha(event.alpha)
          });
          // more event listeners
        }
      }
    }

    然后在您的组件中,确保 @click=listener。您需要添加一些逻辑来围绕该方法开始/停止捕获。您可以使用按钮翻转 continue 属性。

    Antwort
    0
  • StornierenAntwort