【问题标题】:Deeplinks gives empty path深层链接提供空路径
【发布时间】:2019-01-23 11:52:45
【问题描述】:

我在实施 DeepLink 时遇到问题。我可以从 myapp://myapp.com/route 之类的 URL 打开应用程序。但它不处理它的路径。它只是打开程序。

我打开它:

 this.deeplinks.route({
  '/route': RoutePage
    }).subscribe(match => {
  // match.$route - the route we matched, which is the matched entry from the arguments to route()
  // match.$args - the args passed in the link
  // match.$link - the full link data
  console.log('Successfully matched route', match);
}, nomatch => {
  // nomatch.$link - the full link data
  console.error('Got a deeplink that didn\'t match', nomatch);
});

但我没有收到任何控制台日志并且路径为空,只有以下消息:

我的 config.xml

 <plugin name="ionic-plugin-deeplinks" spec="^1.0.17">
    <variable name="URL_SCHEME" value="iskibris" />
    <variable name="DEEPLINK_SCHEME" value="https" />
    <variable name="DEEPLINK_HOST" value="iskibris.com" />
    <variable name="ANDROID_PATH_PREFIX" value="/" />
    <variable name="ANDROID_2_PATH_PREFIX" value="/" />
    <variable name="ANDROID_3_PATH_PREFIX" value="/" />
    <variable name="ANDROID_4_PATH_PREFIX" value="/" />
    <variable name="ANDROID_5_PATH_PREFIX" value="/" />
    <variable name="DEEPLINK_2_SCHEME" value="" />
    <variable name="DEEPLINK_2_HOST" value="" />
    <variable name="DEEPLINK_3_SCHEME" value="" />
    <variable name="DEEPLINK_3_HOST" value="" />
    <variable name="DEEPLINK_4_SCHEME" value="" />
    <variable name="DEEPLINK_4_HOST" value="" />
    <variable name="DEEPLINK_5_SCHEME" value="" />
    <variable name="DEEPLINK_5_HOST" value="" />
</plugin>

调用应用程序iskibris://iskibris.com/route的链接 有人可以帮我吗?

【问题讨论】:

    标签: ionic-framework ionic3 ionic-native


    【解决方案1】:

    Ionic Deeplinks 有两种方法可以打开另一个页面。

    1. 路线
    2. routeWithNavController

    当使用route 方法时,您应该使用 nav.push 导航另一个页面。您可以在您的app.component.ts 中实现它,如下所示。

      @ViewChild(Nav) nav:Nav;
      constructor(private deeplinks: Deeplinks) {
        platform.ready().then(() => {
    
           this.deeplinks.route({
             '/about': AboutPage
           }).subscribe(match => {
             this.nav.push(AboutPage);
           }, nomatch => {
             console.error('Got a deeplink that didn\'t match', nomatch);
          });
    
        });
      }
    }
    

    或者使用routeWithNavController 引用NavController 并处理实际导航。 routeWithNavController 方法可以在您的app.component.ts 中实现如下

          @ViewChild(Nav) nav:Nav;
    
          constructor(private deeplinks: Deeplinks) {
    
            platform.ready().then(() => {
    
             this.deeplinks.routeWithNavController(this.nav, {
                '/about': AboutPage
    
              }).subscribe(match => {
    
                console.log('Successfully matched route', JSON.stringify(match, null, 2));
              }, nomatch => {
    
                console.error('Got a deeplink that didn\'t match', nomatch);
              });
            });  
     }
    

    还有一件事, 在您的 config.xml 中, &lt;variable name="URL_SCHEME" value="iskibris" /&gt; 应改为 &lt;variable name="URL_SCHEME" value="myapp" /&gt;.

    【讨论】:

      【解决方案2】:

      对我来说,奇怪的是,唯一可行的方法是使用 routeWithNavController 方法并在第二个参数上传递一个空对象。顺便说一句,我正在使用 Ionic 3 并在 iPhone 模拟器上进行测试。

      这里是测试:

      /**
       * Using the routeWithNavController method and passing an empty object on the second parameter
       * WORKS!!
       * Did open the app and fired the "match" callback with the info of the clicked link
       */
      this.platform.ready().then(() => {
        this.deeplinks.routeWithNavController(this.nav, {
          // Nothing here. Empty object
        }).subscribe((match) => {
          alert('Match');
          alert(JSON.stringify(match));
        }, (nomatch) => {
          alert('No Match');
          alert(JSON.stringify(nomatch));
        });
      });
      

      以下示例无效

      /**
       * Using the routeWithNavController method and passing an object with a "what would be" route on the second parameter
       * DOESN`T WORK
       * Did open the app but didn't fire any of the callbacks. Neither "match" or "nomatch"
       */
      this.platform.ready().then(() => {
        this.deeplinks.routeWithNavController(this.nav, {
          '/': {}
        }).subscribe((match) => {
          alert('Match');
          alert(JSON.stringify(match));
        }, (nomatch) => {
          alert('No Match');
          alert(JSON.stringify(nomatch));
        });
      });
      
      /**
       * Using the route method and passing an object empty object
       * DOESN'T WORK (kinda)
       * Did open the app and fired the nomatch callback (after all, no route specified)
       */
      this.platform.ready().then(() => {
        this.deeplinks.route({
          // Nothing here. Empty object,
        }).subscribe((match) => {
          alert('Match');
          alert(JSON.stringify(match));
        },
        (nomatch) => {
          alert('No Match');
          alert(JSON.stringify(nomatch));
        });
      });
      
      /**
       * Using the route method and passing object with a "what would be" route on the second parameter
       * By the way, here I tried both links:
       * myapp://myapp.domain.com/
       * myapp://myapp.domain.com
       * DOESN`T WORK
       * Did open the app but didn't fire any of the callbacks. Neither "match" or "nomatch"
       */
      this.platform.ready().then(() => {
        this.deeplinks.route({
          '/': {},
        }).subscribe((match) => {
          alert('Match');
          alert(JSON.stringify(match));
        },
        (nomatch) => {
          alert('No Match');
          alert(JSON.stringify(nomatch));
        });
      });
      

      因为只有第一个有效,我不得不自己处理匹配对象数据并做路由,像这样:

      this.deeplinks.routeWithNavController(this.nav, {}).subscribe((match) => {
        if (match.$link.path == "/list") {
          this.nav.setRoot(ListPage);
        }
      }, (nomatch) => {});
      

      这是我的工作 app.component.ts 的完整代码,带有深层链接:

      import { Component, ViewChild } from '@angular/core';
      import { Nav, Platform } from 'ionic-angular';
      import { StatusBar } from '@ionic-native/status-bar';
      import { SplashScreen } from '@ionic-native/splash-screen';
      import { HomePage } from '../pages/home/home';
      import { ListPage } from '../pages/list/list';
      import { Deeplinks } from '@ionic-native/deeplinks';
      
      @Component({
        templateUrl: 'app.html'
      })
      export class MyApp {
        @ViewChild(Nav) nav: Nav;
      
        rootPage: any = HomePage;
      
        pages: Array<{title: string, component: any}>;
      
        constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public deeplinks: Deeplinks) {
          this.initializeApp();
      
          // used for an example of ngFor and navigation
          this.pages = [
            { title: 'Home', component: HomePage },
            { title: 'List', component: ListPage }
          ];
        }
      
        initializeApp() {
          this.platform.ready().then(() => {
            // Okay, so the platform is ready and our plugins are available.
            // Here you can do any higher level native things you might need.
            this.statusBar.styleDefault();
            this.splashScreen.hide();
      
            this.deeplinks.routeWithNavController(this.nav, {}).subscribe((match) => {
              if (match.$link.path == "/list") {
                this.nav.setRoot(ListPage);
              }
            }, (nomatch) => {});
          });
        }
      
        openPage(page) {
          // Reset the content nav to have just this page
          // we wouldn't want the back button to show in this scenario
          this.nav.setRoot(page.component);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-03-20
        • 1970-01-01
        • 1970-01-01
        • 2020-07-30
        • 1970-01-01
        • 1970-01-01
        • 2016-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多