Reactjs,如何将props.location.pathname与字符串进行比较?

问题描述 投票:0回答:2

在我的路由器中,我需要执行以下操作:

if (props.location.pathname !== '/confirm') {
    // redirect to /confirm to force the user to confirm their email
}

if语句没有按预期运行。

如果我输出:

console.log(props.location.pathname)

我进入控制台。

/confirm

但是,值为'/ confirm'的props.location.pathname/confirm不同

我究竟做错了什么?

javascript reactjs comparator string-comparison
2个回答
2
投票

使用==进行比较时,两个操作数的类型应该相同。确保两者都是字符串类型或更改是否为

if (props.location.pathname != '/confirm') {
// redirect to /confirm to force the user to confirm their email

}


0
投票

要比较React.js中的两个字符串,您可以简单地使用三等号(或===),如下所示:

if (stringTemp === 'desired string'){
      console.log('Equal');
    }
© www.soinside.com 2019 - 2024. All rights reserved.