Angular2深层链接问题
问题描述:
当我请求这样一个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在尝试加载应用程序模块时,不会将基本元素用作应用程序的根目录。
答
好吧,事实证明我对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>
林种惊讶的角度演练没有你设置的baseUrl,因为我不知道怎么会deeplinking结合工作与systemjs – cobolstinks