[英]React application is redirected to unspecified route
I am a newbie with Front-end development and I am trying to learn ReactJS. 我是前端开发的新手,正在尝试学习ReactJS。 In one of my example applications, I am using the "live-server public" command to launch the application on my localhost for development purposes. 在我的示例应用程序之一中,我正在使用“ live-server public”命令在本地主机上启动应用程序以进行开发。
package.json contents package.json内容
{
"name": "indecision-app",
"version": "1.0.0",
"main": "index.js",
"author": "Phani Kumar",
"license": "MIT",
"dependencies": {
"babel-preset-env": "1.5.2",
"babel-preset-react": "6.24.1"
}
}
My application is being compiled using babel and launched automatically by using live-server. 我的应用程序正在使用babel进行编译,并使用实时服务器自动启动。
"use strict";
var app = {
title: 'Indecision App',
subtitle: 'put your life in the hands of a computer',
options: ['one','two']
};
function showOptions(){
if (app.options.length > 0){
return <p>Here are your options</p>
}else {
return <p>No Options</p>
}
}
var template = (
<div>
<h1>{app.title}</h1>
{app.subtitle && <p>{app.subtitle}</p>}
{showOptions()}
<ol>
<li>Item one</li>
<li>Item two</li>
</ol>
</div>
);
var user = {
name: 'phani',
age: 30,
location: 'Bangalore'
};
var userName = 'PhaniKumar Yadavilli';
var age = 30;
var location = "Bangalore";
function getLocation(location){
if (location){
return <p>Location: {location}</p>;
}else {
return undefined;
}
}
var templateTwo = (
<div>
<h1>{user.name ? user.name : 'Anonymous'}</h1>
{(user.age && user.age >= 18) && <p>Age: {user.age}</p>}
{getLocation(user.location)}
</div>
);
var appRoot = document.getElementById('app');
ReactDOM.render(templateTwo, appRoot);
The moment I hit the URL 127.0.0.1:8080 the application is redirected to 127.0.0.1:8080/Bangalore . 我点击URL 127.0.0.1:8080的那一刻,应用程序被重定向到127.0.0.1:8080/Bangalore 。 I tried debugging but I could not understand what is the reason for this redirection. 我尝试调试,但是我不明白进行此重定向的原因是什么。
index.html content index.html内容
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>Indecision App</title>
</head>
<body>
<div id="app"></div>
<script src="https://unpkg.com/react@16.0.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.development.js"></script>
<script src="/scripts/app.js"></script>
</body>
</html>
The statement var location = 'Bangalore';
语句var location = 'Bangalore';
triggers the redirect as location
is a reserved word in JavaScript that represents the browser URL. 触发重定向,因为location
是JavaScript中的保留字,代表浏览器URL。 Rename the variable to something different 将变量重命名为其他名称
Edit: to properly redirect one would do like so: window.location = 'somewhere to go to'
. 编辑:正确地重定向一个将是这样的: window.location = 'somewhere to go to'
。
The var location = ''
is more of a "bug" var location = ''
更像是“虫子”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.