2016-06-14 81 views
2

我有ab Angular 2应用程序。使用路由器不推荐使用(因为新路由器目前没有记录)。有两条基本路线(家庭和管理员)。 如果我加载应用程序或执行F5刷新,则当前模块会加载两次(您可以看到该HTML文件被请求了两次,构造函数也被调用了两次,如何阻止该应用程序加载该路由两次?路线不会造成 “双装载”。只有F5 /弗里斯特负载Angular 2 - 模块在刷新时加载两次

<html> 
<head> 
<base href= /> 
<title>Consyd Time Management</title> 
<meta charset=UTF-8> 
<meta name=viewport content="width=device-width,initial-scale=1"> 
<link rel=stylesheet href=public/css/styles.css>  
<link rel=stylesheet href=node_modules/bootstrap/dist/css/bootstrap.min.css> 
<script src=node_modules/core-js/client/shim.min.js></script> 
<script src=node_modules/zone.js/dist/zone.js></script> 
<script src=node_modules/reflect-metadata/Reflect.js></script> 
<script src=public/scripts/externalLibs.js></script> 
</head> 
<body> 
<div id=main class=container-fluid> <start-page>Loading...</start-page>  </div> 

start.ts

import { Component } from '@angular/core'; 
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from  '@angular/router-deprecated'; 
import { NavigationMenu } from "./navigation"; 
import { myRoutes } from '../routes'; 


@Component({ 
selector: 'start-page', 
template: ` 
<h2>Startseite</h2> 
<navigation-menu></navigation-menu> 
<router-outlet></router-outlet>`, 
directives: [ROUTER_DIRECTIVES, NavigationMenu] 
}) 
@RouteConfig(myRoutes) 

export class StartPage { 

} 

和routes.ts

import { RouteDefinition } from '@angular/router-deprecated'; 
import { HomePage } from './views/home'; 
import { AdminPage} from './views/adminpage'; 


export const myRoutes: RouteDefinition[] = [ 
    { 
     path: '/', 
     name: 'Home', 
     component: HomePage 
    }, 
    { 
     path: '/admin', 
     name: 'Admin', 
     component: AdminPage 

    } 
]; 

home.ts

//imports going here 
@Component({ 
    selector: 'start-page', 
    templateUrl: 'public/app/html/home.html', 
    directives: [DROPDOWN_DIRECTIVES, CORE_DIRECTIVES], 
    providers: [HttpService] 
}) 

export class HomePage { 
    constructor(private httpService: HttpService) { 
     if (!this.selectedProject) { 
      this.selectedProject = new Project("Projekt auswählen"); 
     } 
     this.getUsers(this.setUser); 

    } 
//REST OF CODE 

回答

1

关闭:在index.html的加入externallibs.js两次(一个用手,一个由的WebPack)....

+0

感谢您的想法 - 我加该组件引入NgModules中引导双重加载的引导数组。它应该只在声明中,而不是在自举中。 – DFBerry