首页 > 解决方案 > 写入“npm start”时打开特定窗口

问题描述

我想知道有没有办法在编写“npm start”时让浏览器不打开“localhost:3000”,而是像这样:“localhost:3000/param=1”?而且我不希望它只出现在我的电脑上。我希望当其他人安装我的反应应用程序时,他们也打开一个带有参数的窗口。有没有办法做到这一点?

标签: reactjsnpmcreate-react-app

解决方案


好的,这实际上非常简单。我所需要的只是编写一个 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");
  }

推荐阅读