2016-09-28 5 views
0

Deep-Linking funktioniert, wenn ich eine URL wie diese anfragen mydomain.com/my-appname/aRoute, aber wenn ich gehe zu my-domain.com/my-appname/aRoute/ system.js versucht um nach meiner main.js Datei unter my-domain.com/my-appname/aRoute/app/main.js zu suchen, wenn sie sich hier befindet: my-domain.com/my-appname/app/main.js so ein 404 ist produziert und eckig lädt nie.Angular2 Deep Linking Problem

Hier ist, wie ich versucht, meine app in system.js zu importieren

<script> 
    System.import('app') 
     .catch(function (err) { console.error(err); }); 

</script> 

ich dynamisch bin meine Basis Wurzelelement wie diese Einstellung:

<script> 
     // this function is needed to dynamically determine the root url 
     // the angular2 router uses this to support deep linkinkg. 
     function getDocumentBase() { 
      let loc = document.location.href; 
      let locLower = loc.toLowerCase(); 

      let ind = locLower.indexOf('myappnamehere/') 

      let newLoc = loc.substring(0, (ind + 14)); 
      return newLoc; 
     } 
     document.write('<base href="' + getDocumentBase() + '" />'); 
    </script> 

aber es scheint, dass system.js verwendet das Basiselement nicht als Stammverzeichnis der App, wenn versucht wird, das App-Modul zu laden.

Antwort

0

Ok stellt sich heraus, dass ich nichts von Systemjs wusste, war das Problem. Ich musste meine systemjs.config.js Datei wie folgt ändern:

(function (global) { 
    System.config({ 

     baseURL: getDocumentBase(), 
     paths: { 
      // paths serve as alias 
      'npm:': 'Scripts/npmlibs/' 
     }, 
     // map tells the System loader where to look for things 
     map: { 
      // our app is within the app folder 
      app: 'app', 
      // angular bundles 
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js', 
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js', 
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', 
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', 
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', 
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js', 
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js', 
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', 
      // other libraries 
      'rxjs': 'npm:rxjs', 
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api', 
      'lodash': 'npm:lodash/lodash.js' 
     }, 
     // packages tells the System loader how to load when no filename and/or no extension 
     packages: { 
      app: { 
       main: './main.js', 
       defaultExtension: 'js' 
      }, 
      rxjs: { 
       defaultExtension: 'js' 
      }, 
      'angular2-in-memory-web-api': { 
       main: './index.js', 
       defaultExtension: 'js' 
      }, 
      'lodash': { defaultExtension: 'js' } 
     } 
    }); 
})(this); 

Der wichtigste Teil baseURL ging, ruft sie diese Funktion in meiner Datei index.html:

<script> 
    // this function is needed to dynamically determine the root url 
    // the angular2 router uses this to support deep linkinkg. 
    function getDocumentBase() { 
     let loc = document.location.href; 
     let locLower = loc.toLowerCase(); 

     let ind = locLower.indexOf('my-app-name-here/') 

     // trim off anything after appname as that is truely the root of the app (its a deep link to a route) 
     let newLoc = loc.substring(0, (ind + 17)); 
     return newLoc; 
    } 
</script> 

und ich dann verwenden dass gleiche Funktion das Basiselement so zu machen:

<script> 
    document.write('<base href="' + getDocumentBase() + '" />'); 
</script> 
+0

im Art von suprised die Winkeldurchlauf hat nicht Sie die baseUrl gesetzt, weil ich nicht sicher bin, wie mit Deeplinken systemjs in Verbindung funktionieren würde, – cobolstinks