我的問題回答這裏 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);
});
}
}
哦EventEmitter的伎倆,而不是ReplaySubject ..你救了我的一天男人!謝謝.. – Avijit