gpt4 book ai didi

javascript - 在 React/JS 中打印当前 URL

转载 作者:行者123 更新时间:2023-11-30 15:05:24 26 4
gpt4 key购买 nike

我想在屏幕上打印 URL(是的,它写在 URL 栏中,但我还是想这样做)

我可以使用什么?在下面的示例中,我可以写什么来代替 {iDontKnowWhatToWriteHere} 来给出当前 URL?

import React from 'react';

class Connexion extends React.Component {

render() {
return (
<h1> the URL is {iDontKnowWhatToWriteHere} </h1>
)
}
}

谢谢

最佳答案

React 是一个普通的 javascript 库,它只负责应用程序的 View ,因此它允许您访问所有可用的普通浏览器 API 和属性,其中之一是位置

因此,如果你想访问 React 中的实际 url,你应该以不使用 React 的方式进行操作(除非你使用一些特定的库来解决该问题,例如 React-router),只是:

如果你想获取所有的url(包括域名、协议(protocol)、端口)

window.location.href

或者,如果您只想要 de 域之后的路径

window.location.pathname

在您的代码中

import React from 'react';

class Connexion extends React.Component {

render() {
return (
<h1> the URL is {window.location.pathname} </h1>
)
}
}

关于javascript - 在 React/JS 中打印当前 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45825252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com