代码之家  ›  专栏  ›  技术社区  ›  wandermonk

React应用程序被重定向到未指定的路由

  •  1
  • wandermonk  · 技术社区  · 6 年前

    包.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"
      }
    }
    

    "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);
    

    当我点击网址的那一刻 127.0.0.1:8080 应用程序被重定向到 127.0.0.1:8080/班加罗尔

    索引.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>
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Sven Tschui    6 年前

    声明 var location = 'Bangalore'; location 是JavaScript中表示浏览器URL的保留字。将变量重命名为其他名称


    编辑:要正确重定向,请执行以下操作: window.location = 'somewhere to go to' .

    这个 var location = '' 更像是“虫子”