reactjs - 写入“npm start”时打开特定窗口
问题描述
我想知道有没有办法在编写“npm start”时让浏览器不打开“localhost:3000”,而是像这样:“localhost:3000/param=1”?而且我不希望它只出现在我的电脑上。我希望当其他人安装我的反应应用程序时,他们也打开一个带有参数的窗口。有没有办法做到这一点?
解决方案
好的,这实际上非常简单。我所需要的只是编写一个 JavaScript 文件,当用户在该链接上时将其重定向到链接“localhost:3000/?param=1”,但没有参数。这是javascript代码:
// This is the variable where the url params are stored
const queryString = window.location.search;
console.log(queryString);
// This will catch the letters in the url "?param=1" because 9 is excluded
var firstFewChars = queryString.substring(0,9);
// If there is no characters after the "/" Aka if there is only "localhost:3000" then
// it will redirect the user to the url "localhost:3000/?param=-1"
if(firstFewChars == "") {
window.location.replace("http://localhost:3000/?param=-1");
}
推荐阅读
- java - Eclipse Kura http 身份验证失败
- c# - 从下拉列表中填充 asp.net 表单
- snmp - 为什么 snmpset 不能保存价值?
- maven - 清理和构建 Maven 项目的异常
- java - Maven 资源插件迁移
- java - 使用注解将“yyyy-MM-dd'T'HH:mm:ss”解析为 Kotlin 中的 LocalDateTime
- c# - Windows 10 1803 机器无法从 Windows 2003 网络共享运行 .exe
- pdf - PDF 文件的自定义属性
- python - 如何在python中的字符串中“渲染”\ b
- wpf - WPF 应用程序中的上下文是什么?