gpt4 book ai didi

window.open does not load relative path file in reactjs . Is it possible to load relative path files?(window.open不加载reactjs中的相对路径文件。是否可以加载相对路径文件?)

转载 作者:bug小助手 更新时间:2023-10-28 13:57:55 25 4
gpt4 key购买 nike



I have tried load realtive path file inside the window.open() method but it doesn't loaded. The src folder contains test.html file and test.tsx file

我尝试在window.open()方法中加载相对路径文件,但它没有加载。src文件夹包含test.html文件和test.tsx文件


test.html file

Test.html文件


<!DOCTYPE html> 
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<div>
<h1>Welcome test page</h1>
</div>
</body>


test.tsx file

Test.tsx文件


import React from "react";

export const Test = () => {
const openWindow = () => {
const features =
"menubar=no,location=no,resizable=no,scrollbars=no,status=no,
addressbar=no,width=320,height=480";
// const url = "./test.html";
const url = "http://127.0.0.1:5501/src/test.html";
window.open(url, "self", features);
};
return (
<>
<button onClick={openWindow}>Open Window</button>
</>
);
};

If I tried this way "http://127.0.0.1:5501/src/test.html" it loads correclty and display test.html file output.

如果我尝试这种方式“http://127.0.0.1:5501/src/test.html”,它加载正确的并显示.html文件输出。


output is

输出为


enter image description here


If I tried this way "./test.html" it doesn't work it shows cannot GET /test.html

如果我尝试这种方式“./test.html”,它不起作用,它显示无法获取/test.html


output is

输出为


enter image description here


Is there anyother way to load text.html file ? kindly share your suggestions...

有没有其他方法来加载text.html文件?请分享您的建议…


Thank you!

谢谢大家!


更多回答
优秀答案推荐

there shouldn't be an html file in src, As per my understanding, you need to write HTML inside tsx file and then start the react dev server using npm run start Your test.html should be index.html and should be in the public folder. let me know if it works.

Src中不应该有html文件,根据我的理解,您需要在tsx文件中编写html,然后使用npm run start启动REACTION开发服务器。您的est.html应该是index.html并且应该在公共文件夹中。如果它起作用了,请告诉我。



The relative path will relate to current browser path, not to the tsx file source.

相对路径将与当前浏览器路径相关,而不是与TSX文件源相关。


So, if you your current URL is http://127.0.0.1:5501/ you can use:

因此,如果您当前的网址是http://127.0.0.1:5501/,您可以使用:


window.open('src/test.html')

更多回答

I just moved test.html file to public folder. It works!

我刚刚将test.html文件移到了公共文件夹。它起作用了!

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