2016-12-28 98 views
1

我是新手NS和Angular 2平臺。我嘗試在我的NativeScript Angular 2項目中使用https://github.com/sitefinitysteve/nativescript-google-analytics這個插件。我的項目整個代碼如下。當我測試我的項目時,我給錯誤爲「找不到插件」。將NativeScript插件與NativeScript Angular 2一起使用

我共同的問題:「我可以用每NPM NS模塊與NS角2?」如果我使用我該怎麼做?

app.module.ts

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core"; 
import { NativeScriptModule } from "nativescript-angular/platform"; 
import { NativeScriptHttpModule } from "nativescript-angular/http"; 

import { AppComponent } from "./app.component"; 
import { CountriesComponent } from "./pages/countries.component"; 


@NgModule({ 
    declarations: [AppComponent, CountriesComponent], 
    bootstrap: [AppComponent], 
    imports: [NativeScriptModule, NativeScriptHttpModule], 
    schemas: [NO_ERRORS_SCHEMA] 
}) 
export class AppModule { } 

main.ts

import { platformNativeScriptDynamic } from "nativescript-angular/platform"; 

import { AppModule } from "./app.module"; 


platformNativeScriptDynamic().bootstrapModule(AppModule); 

app.component.ts

import { Component } from "@angular/core"; 

    @Component({ 
     selector: "my-app", 
     template: "<countries></countries>" 
    }) 
    export class AppComponent {} 

個countries.component.ts

import { Component } from "@angular/core"; 
import { JsonService } from '../services/json.service'; 
import { AdmobService } from '../services/admob.service'; 
import { AnalyticsService } from '../services/analytics.service'; 

@Component({ 
    selector: "countries", 
    templateUrl: "pages/countries.component.html", 
    providers: [AdmobService, AnalyticsService] 
}) 
export class CountriesComponent { 

    constructor(private _AdmobService: AdmobService, private _AnalyticsService: AnalyticsService) { 
       _AdmobService.createBanner();   
    } 

    ngOnInit() { 
     this. _AnalyticsService.initAnalytics(); 
    } 
} 

analytics.service.ts

import { Component } from "@angular/core"; 
import { Injectable } from "@angular/core"; 
import * as googleAnalytics from 'nativescript-google-analytics'; 

@Injectable() 
export class AnalyticsService { 

    public constructor() { } 

    public initAnalytics() { 
    googleAnalytics.initalize({ 
     trackingId: 'UA-XXXXXXX-9', 
     dispatchInterval: 5, 
     logging: true 
    }); 
    } 
} 

EDITED

我得到錯誤的波紋管

JS: TypeError: Cannot read property 'GoogleAnalytics' of undefined 
JS:  at Object.exports.initalize (/data/data/org.nativescript.JLB/files/app/tns_modules/nativescript-google-analytics/index.js:10:51) 
JS:  at AppComponent.initAnalytics (/data/data/org.nativescript.JLB/files/app/app.component.js:11:25) 
JS:  at AppComponent.ngOnInit (/data/data/org.nativescript.JLB/files/app/app.component.js:8:14) 
JS:  at Wrapper_AppComponent.ngDoCheck (/AppModule/AppComponent/wrapper.ngfactory.js:22:53) 
JS:  at DebugAppView.View_AppComponent_Host0.detectChangesInternal (/AppModule/AppComponent/host.ngfactory.js:28:26) 
JS:  at DebugAppView.AppView.detectChanges (/data/data/org.nativescript.JLB/files/app/tns_modules/@angular/core/bundles/core.umd.js:9354:18) 
+0

如果你安裝了谷歌分析與'tns plugin add nativescript-google-analytics'導入應該工作。 'tns'將插件安裝到'npm' –

+0

@MaximShoustin是的,我按照你在開始時寫的那樣做。您對我的整個代碼方法有何看法?這樣對嗎? – Kerberos

+0

初始化調用應放在main.ts或main.aot.ts文件之前,自舉行 –

回答

0

它應該是這樣的

import { platformNativeScriptDynamic } from "nativescript-angular/platform"; 
import { AppModule } from "./app.module"; 
import * as googleAnalytics from 'nativescript-google-analytics'; 

googleAnalytics.initalize({ 
    trackingId: 'UA-XXXXXXX-9', 
    dispatchInterval: 5, 
    logging: true 
}); 

platformNativeScriptDynamic().bootstrapModule(AppModule); 
+0

您好,我已將您的建議落實到「main.ts」文件中。但我得到的錯誤是這樣「的未定義無法讀取屬性‘Google分析’」 – Kerberos

+0

我編輯我的職務滿錯誤代碼 – Kerberos

+0

編輯所以它採取與此類似 https://www.npmjs.com/package/nativescript-google -analytics#打字稿 ,並檢查你的SDK安裝 https://www.npmjs.com/package/nativescript-google-analytics#android –

相關問題