2016-08-03 52 views
-1

我想谷歌分析添加到我的項目,但得到一些導航錯誤,我現在用新的路由器,下面是我的代碼:角2 +谷歌分析+路由器導航

// app.component.ts 
declare var ga:Function; 
export class App { 
    private currentRoute:string; 
    constructor(_router: Router, _location: Location) { 
     _router.events.subscribe((event:Event) => { 
      if(event instanceof NavigationStart) { 
       var newRoute = this._location.path() || '/'; 
       if(newRoute !== this.currentRoute) { 
        ga('send', 'pageview', newRoute); 
        this.currentRoute = newRoute; 
       } 
      } 
     }); 
    } 
} 

這是錯誤我越來越:

ERROR in [default] angular/src/app/app.ts:18:27 
Argument of type '(event: Event) => void' is not assignable to parameter of type 'NextObserver<NavigationStart | NavigationEnd | NavigationCancel | NavigationError> | ErrorObserve...'. 
    Type '(event: Event) => void' is not assignable to type '(value: NavigationStart | NavigationEnd | NavigationCancel | NavigationError) => void'. 
    Types of parameters 'event' and 'value' are incompatible. 
     Type 'NavigationStart | NavigationEnd | NavigationCancel | NavigationError' is not assignable to type 'Event'. 
     Type 'NavigationStart' is not assignable to type 'Event'. 
      Property 'bubbles' is missing in type 'NavigationStart'. 

ERROR in [default] angular/src/app/app.ts:19:23 
Cannot find name 'NavigationStart'. 

ERROR in [default] angular/src/app/app.ts:20:24 
Property '_location' does not exist on type 'App'. 
+0

HAV您導入Navig ationStart from @ angular/router和Location from @ angular/common –

+0

請參閱http://stackoverflow.com/a/38448830/652850和http://stackoverflow.com/a/38464412/652850 –

+0

@ArpitAgarwal昨天本身我做了導入NavigationStart&end&解決了我的問題.. :) –

回答

1

請加入import語句,以及看看是否能解決您的問題:

import {Location} from '@angular/common'; 
import {NavigationStart} from '@angular/router';