SNa*_*aRe 2 javascript phpstorm webstorm vue.js
我有一个 Vue 脚本。当我使用 PhpStorm 调试它时,即使我设置了变量redirect_to
. 请检查图像。我该如何解决?
<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>
Run Code Online (Sandbox Code Playgroud)
如果您在代码中使用一些只有在运行时才知道键的对象(通过 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);
}
});
Run Code Online (Sandbox Code Playgroud)
另请参见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其他可能的解决方法