2017-04-03 4 views
0

私は新しいインテントのデータを受け取れるようにアクティビティクラスを拡張しています。これは期待どおりに動作していますが、取得したデータに基づいてアプリケーションをルーティングする方法を把握することができません。アクティビティonNewIntentメソッドでAngularjs Nativescriptアプリを再ルーティングするにはどうすればいいですか(DeepLinking)

import { setActivityCallbacks, AndroidActivityCallbacks } from "ui/frame"; 

@JavaProxy("org.myApp.MainActivity") 
class Activity extends android.app.Activity { 
    private _callbacks: AndroidActivityCallbacks; 

    protected onCreate(savedInstanceState: android.os.Bundle): void { 
     if (!this._callbacks) { 
      setActivityCallbacks(this); 
     } 

     this._callbacks.onCreate(this, savedInstanceState, super.onCreate); 
    } 

    protected onNewIntent(intent: android.content.Intent): void { 
     super.onNewIntent(intent) 
     if (intent.getDataString) { 
     let data = intent.getDataString(); 

     if (data) { 
      console.log('onNewIntent data ', data); 
      //How do I route my Angularjs Nativescript application here based on the data I get back? 
     } 
     } 
    } 
} 

答えて

1

私の質問はここに https://github.com/ddfreiling/tns-ng-deeplink

に答えていた。しかし、ここで私が探していたものの抜粋です。

あなたのactivity.android.tsファイルには、インテントデータを生成するcreateおよびEventEmitterがあります。

import { EventEmitter } from '@angular/core'; 
import { setActivityCallbacks, AndroidActivityCallbacks } from "ui/frame"; 

export var OnRouteToURL = new EventEmitter<string>(); 

@JavaProxy("org.myApp.MainActivity") 
class Activity extends android.app.Activity { 
    private _callbacks: AndroidActivityCallbacks; 

    protected onCreate(savedInstanceState: android.os.Bundle): void { 
     if (!this._callbacks) { 
      setActivityCallbacks(this); 
     } 

     this._callbacks.onCreate(this, savedInstanceState, super.onCreate); 
    } 

    protected onNewIntent(intent: android.content.Intent): void { 
     super.onNewIntent(intent); 


     if (intent.getAction() === android.content.Intent.ACTION_VIEW){ 
      const dataStr = intent.getDataString(); 
      OnRouteToURL.next(dataStr); 
     } 
    } 
} 

そして、あなたのapp.component.tsファイルで、OnRouteToURLイベントエミッタをインポートしてそれを購読してください。

import { Component, OnInit, EventEmitter, NgZone } from "@angular/core"; 
import { Router } from '@angular/router'; 
import * as application from 'application'; 
import { isAndroid, isIOS } from 'platform'; 

let OnRouteToURL: EventEmitter<string>; 
if (isIOS) { 
    application.ios.delegate = require('./delegate').CustomAppDelegate 
    OnRouteToURL = require('./delegate').OnRouteToURL; 
} else if (isAndroid) { 
    OnRouteToURL = require('./activity').OnRouteToURL; 
} 

@Component({ 
    selector: "ns-app", 
    templateUrl: "app.component.html", 
}) 
export class AppComponent implements OnInit { 

    constructor(
     private zone: NgZone, 
     private router: Router 
    ) { 
    } 

    ngOnInit() { 
     // Subscribe to routing events from both platforms 
     OnRouteToURL.subscribe((url) => this.handleRouting(url)); 
    } 

    handleRouting(url: string) { 
     // Assume everything after :// is an app route 
     // in production you might want to limit which routes are allowed to deep-link 
     const route = url.substr(url.indexOf('://') + 3); 
     console.log(`AppComponent: Navigate to route '${route}'`); 

     // Do the routing in the Angular Zone, just to be sure 
     this.zone.run(() => { 
      this.router.navigateByUrl(route); 
     }); 
    } 
} 
+1

Oh EventEmitterはReplaySubjectの代わりにトリックを行います。あなたは私の一日の人を救った!!ありがとう.. – Avijit

関連する問題