我当前在javascript中使用严格的===比较,该比较会在我的计算机上按要求产生预期的结果,但是当部署到服务器时,它的行为并不相同。

脚本

computed: {
            type: function(){
                switch(this.job.type)
                {
                    case 1:
                        return 'Request Quote';
                    case 2:
                        return 'Negotiate';
                    case 3:
                        return 'Fixed';
                    default:
                        return 'matched none';
                }
            },
}


模板

<p class="text-primary"><strong>{{type}}</strong></p>


在本地服务器上输出作业类型为2

javascript - 严格的比较不能在生产环境中产生预期的结果,但是可以在本地服务器上很好地工作-LMLPHP

生产服务器上的作业类型为2的输出
javascript - 严格的比较不能在生产环境中产生预期的结果,但是可以在本地服务器上很好地工作-LMLPHP

如果我将代码切换为松散的比较If语句,则效果很好

如果陈述

computed: {
        type: function(){
            if(this.job.type == 1) return 'Request Quote';
            else if(this.job.type == 2) return 'Negotiate';
            else if(this.job.type == 3) return 'Fixed';
            else return 'matched none';
        },
}


生产服务器中的结果
javascript - 严格的比较不能在生产环境中产生预期的结果,但是可以在本地服务器上很好地工作-LMLPHP

您可能已经注意到,我正在使用VUEJS框架,工作对象也是用axios获取的数据库模型。我还在后端使用Laravel。

mysql版本可能有问题吗?

在本地计算机上运行的版本
javascript - 严格的比较不能在生产环境中产生预期的结果,但是可以在本地服务器上很好地工作-LMLPHP

版本在生产中运行
javascript - 严格的比较不能在生产环境中产生预期的结果,但是可以在本地服务器上很好地工作-LMLPHP

最佳答案

这意味着您的代码/配置/等。生产中正在创建字符串,数字对象(而不是原始对象)或某种其他类型的对象,这些对象在被强制转换为数字时会强制转换为您的期望值之一。

字符串示例:



function strict(type) {
    switch(type)
    {
        case 1:
            return 'Request Quote';
        case 2:
            return 'Negotiate';
        case 3:
            return 'Fixed';
        default:
            return 'matched none';
    }
}

function loose(type) {
    if(type == 1) return 'Request Quote';
    else if(type == 2) return 'Negotiate';
    else if(type == 3) return 'Fixed';
    else return 'matched none';
}

var n;
console.log("Number:");
n = 2;
console.log("strict", strict(n));
console.log("loose", loose(n));

console.log("String:");
n = "2";
console.log("strict", strict(n));
console.log("loose", loose(n));





带有数字对象的示例:



function strict(type) {
    switch(type)
    {
        case 1:
            return 'Request Quote';
        case 2:
            return 'Negotiate';
        case 3:
            return 'Fixed';
        default:
            return 'matched none';
    }
}

function loose(type) {
    if(type == 1) return 'Request Quote';
    else if(type == 2) return 'Negotiate';
    else if(type == 3) return 'Fixed';
    else return 'matched none';
}

var n;
console.log("Primitive:");
n = 2;
console.log("strict", strict(n));
console.log("loose", loose(n));

console.log("Number object:");
n = new Number(2);
console.log("strict", strict(n));
console.log("loose", loose(n));





您必须找出环境/代码/配置中的不同之处,从而导致this.job.type的值类型错误。

07-24 09:43
查看更多