Angular2 深层链接问题

Angular2 Deep Linking Issue

当我请求像这样的 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 并且angular 从不加载。

以下是我尝试在 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);

关键部分是设置 baseURL,它在我的 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>