Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2-刷新页面时,url保持不变,但适当的视图不会';不要上膛_Angular_Angular2 Routing - Fatal编程技术网

Angular2-刷新页面时,url保持不变,但适当的视图不会';不要上膛

Angular2-刷新页面时,url保持不变,但适当的视图不会';不要上膛,angular,angular2-routing,Angular,Angular2 Routing,在Angular2,我面临着这个问题。当你刷新页面时。URL保持不变,但未在RouterOutlet中加载相应的视图 除了刷新页面问题外,一切正常。 应用程序ts import {Component,bind} from 'angular2/core'; import {bootstrap} from 'angular2/platform/browser'; import {FORM_DIRECTIVES} from 'angular2/form'; import{HomeCmp} from

在Angular2,我面临着这个问题。当你刷新页面时。URL保持不变,但未在
RouterOutlet
中加载相应的视图

除了刷新页面问题外,一切正常。

应用程序ts

import {Component,bind} from 'angular2/core';

import {bootstrap} from 'angular2/platform/browser';
import {FORM_DIRECTIVES} from 'angular2/form';
import{HomeCmp} from 'Angular/src/Home/home.ts';
import {ContactUsCmp} from 'Angular/src/ContactUs/contactus.ts';

import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
@Component({
selector: 'micropuppy-app',
templateUrl: "ANGULAR/Templates/home.html",
directives:[HomeCmp,ROUTER_DIRECTIVES,ContactUsCmp],
template: ` <nav>
                    <ul class="nav navbar-nav">
                    **<li><a [routerLink]="['Home']">One</a><hr/></li>
                     <li><a [routerLink]="['ContactUs']">Contact Us</a></li>**
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Technologies <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Angular</a></li>
                            <li><a href="#">.NET</a></li>
                        </ul>
                    </li>
            </ul>
        </nav>

    <router-outlet></router-outlet>`
 })

@RouteConfig([
  {path:'/Home', name: 'Home', component: HomeCmp}
  {path:'/ContactUs', name: 'ContactUs', component: ContactUsCmp}
])

export class MicropuppyApp {
    constructor(){
        console.log("Micropuppy app started");
    }
}
 bootstrap(MicropuppyApp, [ROUTER_PROVIDERS,bind(APP_BASE_HREF).toValue(location.pathname)]);
从'angular2/core'导入{Component,bind};
从'angular2/platform/browser'导入{bootstrap};
从'angular2/FORM'导入{FORM_指令};
从'Angular/src/Home/Home.ts'导入{HomeCmp};
从'Angular/src/ContactUs/ContactUs.ts'导入{ContactUsCmp};
从“angular2/Router”导入{Router、Router_PROVIDERS、RouteConfig、Router_指令、APP_BASE_HREF、LocationStrategy、RouteParams、Router_BINDINGS};
@组成部分({
选择器:“微型小狗应用程序”,
templateUrl:“ANGULAR/Templates/home.html”,
指令:[HomeCmp、ROUTER_指令、ContactUsCmp],
模板:`
    **
` }) @线路图([ {路径:'/Home',名称:'Home',组件:HomeCmp} {路径:'/ContactUs',名称:'ContactUs',组件:ContactUsCmp} ]) 出口级微型木偶{ 构造函数(){ log(“Micropuppy应用程序启动”); } } bootstrap(MicropuppyApp,[ROUTER_PROVIDERS,bind(APP_BASE_HREF).toValue(location.pathname)];
使用默认的路由策略(HTML5历史API),您需要一个服务器配置来将所有路径重定向到HTML入口点文件。使用hashbang方法,没有必要。。。如果要切换到这种方法,只需使用以下代码:

import { bootstrap } from "angular2/platform/browser";
import { provide } from "angular2/core";
import {
  ROUTER_PROVIDERS, LocationStrategy, HashLocationStrategy
} from "angular2/router";

bootstrap(MainApp, [
  ROUTER_PROVIDERS,
  provide(LocationStrategy, {useClass:HashLocationStrategy});
]);
您可以看看有关此问题的以下问题:

希望它能帮助你, 蒂埃里

如果你不想#意思是说,选择“路径定位策略”。它有点类似于HashLocationStratedy。
希望,此链接将对您有所帮助。

我通过将此代码放在
index.html
的顶部,解决了此问题

<script>document.write('<base href="/" />');</script>
document.write(“”);

这不是正确答案,但 在刷新时,您可以通过牺牲404页面将所有死掉的调用重定向到主页 这是一个临时黑客,只需在404.html文件上重播

   <!doctype html>
<html>
    <head>
        <script type="text/javascript">
            window.location.href = "http://" + document.location.host;
        </script>
    </head>
</html>

window.location.href=“http://”+document.location.host;

即使是我也陷入了同样的境地,即刷新Angular2应用程序无法加载

此问题背后的原因是Angular2路由器使用历史API进行路由,因此在刷新路由应用程序时无法找到index.html,最终显示404或无法获取请求的URL

您可以通过两种方式解决此问题:

  • Hashbang技术使用HashLocationStrategy,但由于存在哈希,URL看起来不好

  • 下面是我的计算方法:

  • 我使用Node.js为请求提供服务

    假设已安装节点和Npm

    package.json用于安装所需模块

    {
      "name": "Express server",
      "version": "",
      "dependencies": {
        "express": "^4.14.0"
      }
    }
    
    在项目目录中创建server.js

    var express = require('express');
    var app = express();
    
    app.use(express.static('dist')); 
    // 'dist' is my build folder, you can add yours. this will take care of all static files.
    
    app.use('/*', express.static('dist/index.html')); 
    // This will make sure that every route should serve index.html first 
    
    app.listen(8080, function () {
        console.log('app listening on port 8080!!')
    });
    
    使用以下命令启动应用程序服务器

    node server.js
    

    现在,每个请求都将通过你的应用程序服务器,它将确保每次刷新/重新加载index.html时都能得到服务,这将提升angular应用程序。

    它会按预期工作。但使用这种方法,它添加了
    #
    符号,这对于实际应用程序来说并不合适。我已经添加了
    。请指导。是的,事实上,拥有#符号是因为在服务器端无事可做的奖赏:-(我同意你的观点,hashbang方法的URL不如
    PathLocationStrategy
    (默认设置)干净。但如果您需要更晚的版本,则必须在服务器端提供重定向功能。这取决于您的服务器平台。Firebase只需几行代码即可完成此操作;-)可能重复我认为您遗漏了某些内容。请按照该链接中提到的步骤进行操作。对我来说很好,你很棒。谢谢