2017-04-03 68 views
0

我正在擴展Activity類,以便可以接收新的intent數據。這是按預期工作,但我無法弄清楚如何根據我得到的數據路由我的應用程序。如何在活動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文件中創建併發出意圖數據的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 eventEmitter並訂閱它。

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

哦EventEmitter的伎倆,而不是ReplaySubject ..你救了我的一天男人!謝謝.. – Avijit

相關問題