Javascript 如何在将道具(首次)传递给Vue.js中的组件时调用方法 从“洛达斯”进口; 导出默认值{ 名称:“问题底部”, 道具:{ 问题:反对,, 下一个问题:函数, 增量:函数, }, 数据(){ 返回{ selectedIndex:null, 更正索引:空, shuffleArray:[], 安萨雷:[], }; }, //观察并侦听道具的更改 观察:{ 当前问题(){ this.selectedIndex=null; }, //我希望在第一次将道具传递给组件时运行此方法 allOptions(){ console.log(“我是第二”); log(“this.allOptions中有什么”,this.allOptions); this.correctIndex=this.allOptions.indexOf( 这个.currentQuestion.correct\u答案 ); log(“正确索引isss”,this.correctIndex); }, }, 计算:{ allOptions(){ 设allOptions=[ …此.currentQuestion.答案不正确, 这个.currentQuestion.correct\u答案, ]; //log(“未洗牌的数组是”,allOptions); allOptions=\随机播放(allOptions); log(“无序数组是”,allOptions); //console.log(“Corect ans is”,this.currentQuestion.correct\u response); console.log(“我是第一”); 返回允许选项; }, }, 方法:{ 选定对象(索引){ this.selectedIndex=索引; log(“所选答案索引”,this.selectedIndex); }, 提交人(){ 让我纠正=错误; if(this.selectedIndex==this.correctedIndex){ isCorrect=正确; } 这个增量(是正确的); }, }, };
我想在道具第一次通过时运行我的Javascript 如何在将道具(首次)传递给Vue.js中的组件时调用方法 从“洛达斯”进口; 导出默认值{ 名称:“问题底部”, 道具:{ 问题:反对,, 下一个问题:函数, 增量:函数, }, 数据(){ 返回{ selectedIndex:null, 更正索引:空, shuffleArray:[], 安萨雷:[], }; }, //观察并侦听道具的更改 观察:{ 当前问题(){ this.selectedIndex=null; }, //我希望在第一次将道具传递给组件时运行此方法 allOptions(){ console.log(“我是第二”); log(“this.allOptions中有什么”,this.allOptions); this.correctIndex=this.allOptions.indexOf( 这个.currentQuestion.correct\u答案 ); log(“正确索引isss”,this.correctIndex); }, }, 计算:{ allOptions(){ 设allOptions=[ …此.currentQuestion.答案不正确, 这个.currentQuestion.correct\u答案, ]; //log(“未洗牌的数组是”,allOptions); allOptions=\随机播放(allOptions); log(“无序数组是”,allOptions); //console.log(“Corect ans is”,this.currentQuestion.correct\u response); console.log(“我是第一”); 返回允许选项; }, }, 方法:{ 选定对象(索引){ this.selectedIndex=索引; log(“所选答案索引”,this.selectedIndex); }, 提交人(){ 让我纠正=错误; if(this.selectedIndex==this.correctedIndex){ isCorrect=正确; } 这个增量(是正确的); }, }, };,javascript,vue.js,vue-component,Javascript,Vue.js,Vue Component,我想在道具第一次通过时运行我的watch区域的allOptions()。现在,我知道watch在方法或道具更改时运行,但是我可以从第一次传递道具时开始运行该方法。是否有任何方法可以使此方法从道具传递到组件的点开始运行。使用: 导出默认值{ 观察:{ 允许选项:{ handler(){ this.correctIndex=this.allOptions.indexOf(this.currentQuestion.correct\u答案); }, 立即:是的, } } } 我认为在这种情况下,您可以
watch
区域的allOptions()
。现在,我知道watch
在方法或道具更改时运行,但是我可以从第一次传递道具时开始运行该方法。是否有任何方法可以使此方法从道具传递到组件的点开始运行。使用:
导出默认值{
观察:{
允许选项:{
handler(){
this.correctIndex=this.allOptions.indexOf(this.currentQuestion.correct\u答案);
},
立即:是的,
}
}
}
我认为在这种情况下,您可以使用挂载钩子并调用其中的方法。
<script>
import _ from "lodash";
export default {
name: "QuestionBottom",
props: {
currentQuestion: Object,
nextQuestion: Function,
increment: Function,
},
data() {
return {
selectedIndex: null,
correctIndex: null,
shuffleArray: [],
ansArray: [],
};
},
// watch listens for changes to the props
watch: {
currentQuestion() {
this.selectedIndex = null;
},
// I want this method to run when props are passed to the component even at the first time
allOptions() {
console.log("I am second");
console.log("what's in this.allOptions", this.allOptions);
this.correctIndex = this.allOptions.indexOf(
this.currentQuestion.correct_answer
);
console.log("Correct index isss", this.correctIndex);
},
},
computed: {
allOptions() {
let allOptions = [
...this.currentQuestion.incorrect_answers,
this.currentQuestion.correct_answer,
];
// console.log("array that is not shuffled is ", allOptions);
allOptions = _.shuffle(allOptions);
console.log("shuffled array is", allOptions);
// console.log("Corect ans is ", this.currentQuestion.correct_answer);
console.log("I am first");
return allOptions;
},
},
methods: {
selectedAns(index) {
this.selectedIndex = index;
console.log("Selected answer index", this.selectedIndex);
},
submitAnswer() {
let isCorrect = false;
if (this.selectedIndex === this.correctIndex) {
isCorrect = true;
}
this.increment(isCorrect);
},
},
};
</script>