【问题标题】:Using NativeScript plugin with NativeScript Angular 2在 NativeScript Angular 2 中使用 NativeScript 插件
【发布时间】:2016-12-28 15:09:01
【问题描述】:

我是新手 NS 和 Angular 2 平台。我尝试在我的 NativeScript Angular 2 项目中使用 https://github.com/sitefinitysteve/nativescript-google-analytics 这个插件。我的项目整个代码如下。当我测试我的项目时,我给出的错误是“找不到插件”。

我的常见问题“我可以将每个 NPM NS 模块与 NS Angular 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 { }

ma​​in.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
    });
  }
}

已编辑

我收到如下错误

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)

【问题讨论】:

  • 如果您使用tns plugin add nativescript-google-analytics 安装了谷歌分析,则导入应该可以工作。 tns 将插件安装到 npm
  • @MaximShoustin 是的,我按照你在开头写的那样做。您对我的整个代码方法有何看法?对吗?
  • 初始化调用应该放在 main.ts 或 main.aot.ts 文件中,在 boostrap 行之前
  • @MarekMaszay 我没有放 main.ts 我收到打字稿语法错误你能提供代码示例吗?

标签: angular nativescript angular2-nativescript


【解决方案1】:

应该是这样的

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);

【讨论】:

【解决方案2】:

检查插件使用的节点模块版本,您可以通过任何方式使用集成该功能的nativescript-plugin-firebase

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多