javascript - Vue 未解析的变量 PhpStorm

标签 javascript vue.js phpstorm webstorm

我有一个 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>

enter image description here

最佳答案

如果您在代码中使用某些对象,其键仅在运行时已知(生成、通过 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/

相关文章:

html - PhpStorm - html 类建议

phpstorm - 如何制作Windows快捷方式来打开特定项目?

JavaScript 抓取多维数组的某些值

Firefox 4 和 5 : invalid 'in' operand this. _sandBox - 资源 ://gre/components/nsProxyAutoConfig. js

javascript - 使用Raphael JS矢量图形库-帮助解释示例

javascript - 在具有未定义响应的 VueJs 中发布请求

ajax - 我如何从 this.$http.get 在 vue js 中返回数据

javascript - ng-click 事件 AngularJS 的捕获时间 - 这可能吗?

javascript - Vue : standard way to hide template if no data or loading

css - Simples(LESS自动编译成CSS后弹窗)