我有一个 Vue 脚本。当我使用 PhpStorm 对其进行调试时,即使我设置了变量 redirect_to
,我仍然遇到 Unresolved 变量语法错误。请检查图像。我该如何解决?
<script>
const timeToRefetch = 2000;
let intervalRef;
const homePageUrl = "/";
let messageCount = 0;
var app = new Vue({
el: "#app",
data: {
status: "",
message: "",
redirect_to: ""
},
created() {
const refThis = this;
intervalRef = setInterval(() => {
fetch(ApiUrl)
.then(response => response.json())
.then(repos => {
refThis.status = repos.status;
this.message = repos.message;
clearInterval(intervalRef);
setTimeout(() => {
window.location.href = repos.redirect_to;
}, 3000);
},
);
}, timeToRefetch);
}
});
</script>
最佳答案
如果您在代码中使用某些对象,其键仅在运行时已知(生成、通过 ajax 调用等接收),则 IDE 无法使用静态代码分析来解析它们。但是您可以让 IDE 知道您的运行时数据是什么样的。使用 JSDoc 注释的可能解决方案:
/**
* @typedef {Object} repos
* @property {string} status
* @property {string} message
* @property {string} redirect_to
*
*/
...
var app = new Vue({
el: "#app",
data: {
status: "",
message: "",
redirect_to: ""
},
created() {
const refThis = this;
intervalRef = setInterval(() => {
fetch(ApiUrl)
.then(response => response.json())
.then(
/**
* @param {repos} repos
*/
(repos) => {
refThis.status = repos.status;
this.message = repos.message;
clearInterval(intervalRef);
setTimeout(() => {
window.location.href = repos.redirect_to;
}, 3000);
},
);
}, timeToRefetch);
}
});
另见 https://youtrack.jetbrains.com/issue/WEB-17419#comment=27-1058451 , https://intellij-support.jetbrains.com/hc/en-us/community/posts/206349469-disable-unresolved-variable-on-json-object-received-by-ajax-call其他可能的解决方法
关于javascript - Vue 未解析的变量 PhpStorm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56819062/