【发布时间】:2017-08-31 03:58:07
【问题描述】:
我有一个包含 3 个区域的图像。当我点击每个区域时,我希望出现一系列问题。我已经这样做了,但我想稍微改变一下。 由于我不想将其重定向到页面,因此我将 # 作为 href 链接,并且我正在获取基于 event.currentTarget.id 的区域的 id 然后我有三个 v-if,每个组件都有一个条件。
这是 jfiddle: https://jsfiddle.net/rau4apyg/
<div id="app">
<img id="Image-Maps-Com-image-maps-2017-03-16-100553" src="http://www.image-maps.com/m/private/0/fdfutap3klci37abfmuasc2mk7_screenshot.png" border="0" width="588" height="414" orgWidth="588" orgHeight="414" usemap="#image-maps-2017-03-16-100553" alt="" />
<map name="image-maps-2017-03-16-100553" id="ImageMapsCom-image-maps-2017-03-16-100553">
<area shape="rect" coords="586,412,588,414" alt="Image Map" style="outline:none;" title="Image Map" href="http://www.image-maps.com/index.php?aff=mapped_users_0" />
<area id="component1" alt="" title="comp1" href="#" shape="poly" coords="420,228,296,34,180,226,178,228" style="outline:none;" target="_self" v-on:click="compId($event)" />
<area id="component2" alt="" title="comp2" href="#" shape="poly" coords="92,368,176,234,292,234,294,368,298,236,290,368" style="outline:none;" target="_self" v-on:click="compId($event)" />
<area id="component3" alt="" title="comp3" href="#" shape="poly" coords="506,366,296,366,296,232,422,232" style="outline:none;" target="_self" v-on:click="compId($event)" />
</map>
<h1> Title here </h1>
<div v-if="compid === 'component1'">
component1 is clicked, questions are shown
</div>
<!-- show questions in for loop -->
<div v-if="compid === 'component2'">
2 is clicked
</div>
<div v-if="compid === 'component3'">
3 is clicked
</div>
<div v-show="questionIndex === quiz.questions.length -1">
<button v-on:click="addAnswers">
submit
</button>
<h2>
Quiz finished, plase continue with Component 2 questions.
</h2>
<button v-on:click="goToNextComponent">
Next
</button>
</div>
new Vue({
el: '#app',
data: {
quiz: {
questions: [],
answers: []
},
// Store current question index
questionIndex: 0,
total:0,
show: true,
compid: '',
flag: false
},
mounted: {
//functions here
},
computed: {
//functions here
},
// The view will trigger these methods on click
methods: {
//some functions
//return id of clicked component
compId: function(event){
this.compid = event.currentTarget.id;
console.log(event.currentTarget.id); // returns the name of the id clicked.
} ,
addAnswers: function(){
//store answers in Firebase
//vm.$forceUpdate();
flag = true; //change the flag
console.log(flag);
},
goToNextComponent: function(){
}
}
});
我想按顺序完成问题,即:先做Component1的问题,点击提交保存答案,然后显示Component2的问题,回答完然后移动到Component3。
如果用户完成了组件 1,我希望他不能再次回答这些问题,以某种方式禁用它,然后转到组件 2。 当他完成下一个组件时,我也想禁用它并转到最后一个。
我不知道如何使它以这种方式工作。我有两个想法: 1)当我单击提交按钮时,我将一个标志更改为真。所以我知道组件 1 被点击,我将它添加到 v-if 子句中。我尝试使用 && 运算符添加它,但它不起作用。 2)提交后有一个下一步按钮(我不确定它是否听起来不错),当点击它时,显示组件2中包含的下一个问题。
P.S.我的数据库在 Firebase 上,我有一个数组中的所有问题。例如前 10 个问题属于组件 1,接下来是组件 2 的 8 个,等等。也许添加一个字段来分隔它们会更好吗?现在是这样的:
{
"questions" : [ {
"q_options" : [ "Yes", "No", "Don't know" ],
"q_text" : "Do you agree with blah blah?"
}}
也许我可以添加一个 component_option: 1 您有什么方法可以解决这些问题?
【问题讨论】:
-
您只是希望它们无法返回到已完成的组件,还是希望它们按顺序运行?
-
我的首要任务是不能回到已完成的组件。
标签: html vue.js vuejs2 imagemap