【发布时间】:2021-07-04 03:15:08
【问题描述】:
我在我的 Vue.js Web 应用程序中使用下面的结构。我现在正在尝试对其进行测试。但是在尝试测试exampleOfFunction 时,它说this.exampleOfData2 是undefined。
<template>
*Some HTML*
</template>
<script>
*Some Imports*
export default {
data() {
return {
exampleOfData1: [],
exampleOfData2: 100
},
methods: {
exampleOfFunction:function(){
if(this.exampleOfData2 === 100)
{
return false;
}
return true;
},
created() {
},
mounted() {
}
}
</script>
然后在我的测试文件中,我尝试访问上面的代码,我成功使用console.log(FileToTest.data()); 我可以看到数据的值,我可以使用FileToTest.methods.exampleOfFunction(); 访问该函数,但是当我调用该函数时,它说this.exampleOfData2是undefined。
【问题讨论】:
-
FileToTest到底是什么?如果这是您导入的组件定义,那么您正在以错误的方式对其进行测试。 Vue 在您不这样做时将方法绑定到实例,在不符合实际行为的测试中没有多大价值。