location.href打开链接失败

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m_review/article/details/83311625

window.location.href

错误代码:

window.location.href = `${window.location.host}/detail`;

没有打开相应的路由。 有没有看出来是哪里错了。。。

原因就是:没有加协议 !!!。

正确代码:

// 1.
window.location.href = `http://${window.location.host}/detail`;
// 2.
window.location.href = `/detail`;
// 3.
window.location.href = `//${window.location.host}/detail`;

location是window对象的属性,而所有的网页下的对象都是属于window作用域链中(这是顶级作用域),所以使用时是可以省略window

猜你喜欢

转载自blog.csdn.net/m_review/article/details/83311625