如何在 Activities onNewIntent 方法(DeepLinking)中重新路由我的 Angularjs Nativescript 应用程序

How do I re-route my Angularjs Nativescript app in the Activities onNewIntent method (DeepLinking)

我正在扩展 Activity class 以便我可以接收新的意图数据。这按预期工作,但我无法弄清楚如何根据我获得的数据路由我的应用程序。

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?
        }
      }
    }
}

我的问题在这里得到了回答 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);
        });
    }
}