我有一个不明白的问题:
只是我要显示一些来自 API 的数据,它一切正常,但我收到一个错误 personnel.departmentID
是 undefined
,而且我的 vue-debug 工具不起作用。
仅当我从 departmentID
中分配任何内容时才得到它,而其余部分如 firstName、lastName 等。
奇怪的是 departmentID.name
等的数据显示正常,但会引发以下错误。
在我的控制台中出现错误:
Uncaught TypeError: _ctx.personnel.departmentID is undefined
render edit_personnel.vue:64
renderComponentRoot runtime-core.esm-bundler.js:846
componentEffect runtime-core.esm-bundler.js:4280
reactiveEffect reactivity.esm-bundler.js:42
effect reactivity.esm-bundler.js:17
setupRenderEffect runtime-core.esm-bundler.js:4263
mountComponent runtime-core.esm-bundler.js:4222
processComponent runtime-core.esm-bundler.js:4182
patch runtime-core.esm-bundler.js:3791
render runtime-core.esm-bundler.js:4883
mount runtime-core.esm-bundler.js:3077
mount runtime-dom.esm-bundler.js:1259
js personnel_edit.js:4
Webpack 7
值其正确显示
input displayed with correct data
<input type="text" class="form-control" v-model="personnel.departmentID.name" :key="personnel.departmentID.name" />
</form>
</div>
</template>
<script>
export default {
name: "edit_personnel",
data: () => ({
personnel: [],
departments: [],
location: [],
loaded: false,
}),
async created() {
await fetch(window.currentUserId)
.then(response => response.json())
.then(data => {
this.personnel = data;
this.loaded = true;
});
}
}
</script>
答案 0 :(得分:4)
由于您的 function doubleChar(str) {
var splt = str.split("");
for(var i=0;i<splt.length-1;i++){
var rpt= splt[i].repeat(2);
return rpt.join('');
}
}
console.log("hello");
数据是 personnel
操作,因此您应该在 async
上使用 v-if
指令以防止它在加载时出错。
将您的数据固定为对象而不是数组,
input
并且您的模板应该更改为,
personnel: {}
答案 1 :(得分:1)
您必须像这样初始化 personnel
属性:
data: () => ({
personnel: {
departmentID:{name:'' }
},
departments: [],
location: [],
loaded: false,
}),
async c
因为 departmentID
在第一次渲染时未定义。