Angular2深度链接问题

时间:2016-09-28 18:14:26

标签: angular angular2-routing systemjs

深度链接在我请求这样的网址时起作用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并且从不设置角度负载。

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

但是在尝试加载app模块时,似乎system.js没有使用base元素作为应用程序的根目录。

1 个答案:

答案 0 :(得分: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);

关键部分是设置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>