2016-06-14 84 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