【问题标题】:Vue emit will not be important in other componentVue emit 在其他组件中并不重要
【发布时间】:2020-08-27 15:34:43
【问题描述】:

我正在制作一个 vue 历史测验应用程序,我的问题是从 API 中检索出来的。我想在 ResultScreen 视图中显示问题、正确答案和用户的答案。我采取的方法是将问题推入一个数组并 $emit 它。我可以看到它存储在我的 vue 工具中,但由于某种原因,它没有显示在我想要显示的组件中,因此没有显示在 resultScreeen 中。

currentQuestion 是一个包含问题和答案的对象。

[发出数组的代码。]

storeQuestion: function (value) {
      value = this.submitData.questionArray1
      this.$emit('storeData', value)
      console.log(value, 'my pants are on fire')
    }

shows vue tools at it has been stored.

[我要投影的组件和不会显示问题的功能。]

 <template>
  <div>
    <QuizQuestion
      v-if="questions.length"
      :currentQuestion="questions[index]"
      @storeData="storeQuestion"
                  />
    <p v-if="questions">{{ questions }}</p>
    <p v-else=""> loading.....</p>
  </div>
</template>
<script>
// import { mapGetters } from 'vuex'
import QuizQuestion from '@/components/QuizQuestion.vue'
// se på gameplay hvor vi returner numcorrect, numtotal og numpoints, vi må se på hvordan vi kan importere verde
export default {
  name: 'GameOver',
  components: {
    QuizQuestion
  },
  data () {
    return {
      questions: ''
    }
  },
  methods: {
    storeQuestion: function (value) {
      this.questions = value
      alert(value)
    }
  }
}
</script>


this.submitData.questionArray1.push(this.currentQuestion.question)

【问题讨论】:

  • 与其链接到将来可能不存在的外部图像,不如复制粘贴到您的问题中。 ??????????
  • 好的抱歉,先在这里发帖

标签: vue.js eventemitter emit


【解决方案1】:

问题出在你的模板上,特别是这一行:

<p v-if="questions">{{ storeQuestion() }}</p>

因为storeQuestion方法没有return一个字符串值,所以不管questions的值是什么,它都会返回undefined

如果不知道存储在 questions 中的数据的形状,很难确切知道您想要什么代码,但我怀疑您想要类似的代码:

<p v-if="questions">{{ questions.text }}</p>

...或者也许...

<p v-if="questions">{{ questions[index].text }}</p>

...或类似的。

但是,除了您关于如何显示某些内容的问题之外,您的数据结构让我感到困惑。我希望一个名为questions 的数据变量是一个数组,而不是对象(尤其是因为您使用的是数组访问方法(例如.lengthquestions[index])。我怀疑你会有一些额外的变化制作,但这超出了您关于在 v-if 语句中显示信息的问题的范围。

【讨论】:

  • 所以如果我理解正确,我应该将问题对象更改为字符串?即使我从组件传递的数据是一个数组?
  • 不,我的意思是,模板中{{ whatever }} 之间的任何内容都必须是字符串。目前storeQuestion()的返回值为undefined。如果您提供了有关数据当前形状的更多信息,我可以更具体地说明它可能/应该是什么。
  • 好的,我会尽量告诉你细节,问我是否忘记了什么。我正在从 API 检索问题,它们被投影到一个测验问题组件上,该组件在 gamePlay 视图上显示它。我正在尝试获取我设法在 resultScreen 视图的 gamePlay 视图中投影的信息。我已经将这些信息推送到数组中,当我 console.log 它们工作时。当我尝试在 GameOver 组件中使用 console.log 值时,它是未定义的。所以我认为问题在于我如何检索存储的数据。你怎么看?并感谢您到目前为止的帮助:)
  • 当我问“数据的形状”时,我问的是它是一个数组还是一个对象,以及对象的键是什么。一个有用的答案是[ {firstName: "Steve", lastName: "Jones"} ]。我了解您的数据是问题而不是名称。这只是一个例子。
  • 现在,您将模板更改为什么?同样,storeQuestion() 不应出现在您的模板中。
猜你喜欢
  • 2018-03-05
  • 2018-09-27
  • 2020-03-18
  • 1970-01-01
  • 2021-03-27
  • 2020-04-09
  • 1970-01-01
  • 2020-05-19
  • 2017-11-21
相关资源
最近更新 更多