2016-09-28 142 views
0

當我請求這樣一個url my-domain.com/my-appname/aRoute時,深度鏈接工作,但是當我去my-domain.com/my-appname/aRoute/ system.js嘗試當它位於這裏時,在my-domain.com/my-appname/aRoute/app/main.js下查找我的main.js文件:my-domain.com/my-appname/app/main.js所以404是產生和角度永不加載。Angular2深層鏈接問題

這裏是我試圖導入我的應用程序在system.js

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

</script> 

我動態設置我的基根元素是這樣的:

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

但似乎system.js在嘗試加載應用程序模塊時,不會將基本元素用作應用程序的根目錄。

回答

0

好吧,事實證明我對systemjs一無所知是個問題。我不得不改變我的systemjs.config.js文件,如下所示:

(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); 

的關鍵部分是設置基本URL,它會調用這個函數在我的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> 

,然後我使用同樣的功能來呈現這樣的基本元素:

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

林種驚訝的角度演練沒有你設置的baseUrl,因爲我不知道怎麼會deeplinking結合工作與systemjs – cobolstinks