Vue.js迁移Codepen测试代码时出现quiz变量未定义报错如何解决
问题解决
报错根源
quiz未定义报错是典型的变量作用域问题:你在onWindowLoad方法内部用let声明的quiz是仅在该方法内可用的局部变量,data函数的执行上下文和它完全独立,无法访问到这个变量,因此data中写quiz: quiz时就会触发未定义报错。- 你用
console.log(quiz.title)解决第一个报错只是绕开了ESLint的校验,并不是正确解法,本质是quiz的定义位置完全错误。 - 你的代码还有多处Vue语法错误:
- 生命周期钩子名称错误,应为
created而非create - 重复声明
methods字段:Vue组件的methods选项直接接收方法对象,不需要用函数返回,也不能重复定义两次 - 模板末尾多了一个多余的
</div>闭合标签 - 不需要监听
window.load事件,Vue单文件组件在created钩子执行时已经完成了数据初始化,完全不需要等窗口加载事件触发再赋值静态数据。
修复方案
直接将quiz静态数据移到data选项内部定义,删除无效的事件监听和重复的methods声明即可,修改后的完整代码如下:
模板部分(删除多余闭合标签)
<template> <div id="app" v-cloak> <div class="row"> <div class="large-12 columns"> <h1>{{ quiz.title }}</h1> <div class="callout"> <div v-for="(question, index) in quiz.questions" v-bind:key="question.id"> <div v-show="index === questionIndex"> <h3>{{ question.text }}</h3> <ol> <li v-for="response in question.responses" v-bind:key="response.id"> <label> <input type="radio" v-bind:value="response.value" v-bind:name="index" v-model="userResponses[index]">{{response.text}} </label> </li> </ol> <button class="secondary button" v-if="questionIndex > 0" v-on:click="prev">prev</button> <button class="success button" v-on:click="next">next</button> </div> </div> <div v-show="questionIndex === quiz.questions.length"> <h3>Your Results</h3> <p>You are: {{ score() }}</p> </div> </div> </div> </div> </template>
脚本部分
<script> export default { name: 'PersonalityTest', data() { return { quiz: { title: 'What superhero are you?', questions: [ { id: 1, text: "How would you describe your personality?", responses: [ { text: 'Im serious and dark', value: 'Batman' }, { text: 'Arrogant, but charming', value: 'Superman' }, { text: 'Fun and easy going', value: 'The Flash' } ] }, { id: 2, text: "Why did you want to become a superhero?", responses: [ { text: 'For the thrills', value: 'The Flash' }, { text: 'For justice', value: 'Batman' }, { text: 'For popularity', value: 'Superman' } ] }, { id: 3, text: "Who would you most hang around with?", responses: [ { text: 'Wonder Woman', value: 'Superman' }, { text: 'Green Arrow', value: 'The Flash' }, { text: 'Robin', value: 'Batman' } ] }, { id: 4, text: "What's your favourite colour?", responses: [ { text: 'Black', value: 'Batman' }, { text: 'Red', value: 'The Flash' }, { text: 'Blue', value: 'Superman' } ] }, { id: 5, text: "When do you help people?", responses: [ { text: 'Every chance I can', value: 'The Flash' }, { text: 'At night', value: 'Batman' }, { text: 'When they need me to', value: 'Superman' } ] } ] }, questionIndex: 0, userResponses: [] } }, methods: { next() { this.questionIndex++ console.log(this.userResponses) }, prev() { this.questionIndex-- }, score() { const modeMap = {} let maxEl = this.userResponses[0], maxCount = 1 for (let i = 0; i < this.userResponses.length; i++) { const el = this.userResponses[i] modeMap[el] = modeMap[el] ? modeMap[el] + 1 : 1 if (modeMap[el] > maxCount) { maxEl = el maxCount = modeMap[el] } } return maxEl } } } </script>
修改后两个ESLint报错都会消失,功能也可以正常运行。
内容的提问来源于stack exchange,提问作者HStoltz
相关产品推荐
相关产品推荐

