Vue无法解析的变量PhpStorm

时间:2019-06-29 16:19:32

标签: 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

1 个答案:

答案 0 :(得分:1)

如果您将某些对象与仅在运行时才知道的键一起使用(生成,通过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-1058451https://intellij-support.jetbrains.com/hc/en-us/community/posts/206349469-disable-unresolved-variable-on-json-object-received-by-ajax-call