Angular2路由 - 使用LocationStrategy添加标签不起作用?

时间:2016-03-13 11:05:33

标签: url angular hashtag angular2-routing

我正在关注Angular2文档中的简单快速启动应用程序,并且我使用弹簧后端来运行它。我的问题是,角度路由器从URL中删除了主题标签,所以应该example.com/#/dashboard现在是example.com/dashboard

我正在使用StackOverflow上一堆帖子中指定的LocationStrategy方法。以下是我的简单示例:

File: main.ts

///<reference path="../node_modules/angular2/typings/browser.d.ts"/>

import {bootstrap} from 'angular2/platform/browser'
import {provide} from 'angular2/core';
import {LocationStrategy, HashLocationStrategy} from 'angular2/router';

import {TestComponent} from './simple/test.component'

bootstrap(
    TestComponent, 
    [
        provide(LocationStrategy, { useClass: HashLocationStrategy })
    ]
);

File: test.component.ts

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';

@Component({
    selector: 'test1',
    template: "<h1>This is test1 component</h1>"
})
export class Test1 { };

@Component({
    selector: 'test2',
    template: "<h1>This is test2 component</h1>"
})
export class Test2 { };

@Component({
    selector: 'my-app',

    template: `
        <h1>This is my test app</h1>
        <nav>
            <a [routerLink]="['Test1']">Test1</a>
            <a [routerLink]="['Test2']">Test2</a>
        </nav>
        <router-outlet></router-outlet>
    `,

    directives: [ROUTER_DIRECTIVES],

    providers: [ROUTER_PROVIDERS]
})
@RouteConfig([
    {
        path: '/test1',
        name: 'Test1',
        component: Test1
    },
    {
        path: '/test2',
        name: 'Test2',
        component: Test2
    }
])
export class TestComponent { }

File: index.html

<html>
    <head>

        <base href="/">

        <title>This is an Angular 2 test</title>

        <!-- Angular dependencies -->
        <script src="/node_modules/es6-shim/es6-shim.js"></script>
        <script src="/node_modules/systemjs/dist/system-polyfills.js"></script>
        <script src="/node_modules/angular2/bundles/angular2-polyfills.js"></script>
        <script src="/node_modules/systemjs/dist/system.src.js"></script>
        <script src="/node_modules/rxjs/bundles/Rx.js"></script>
        <script src="/node_modules/angular2/bundles/angular2.dev.js"></script>
        <script src="/node_modules/angular2/bundles/router.dev.js"></script>

        <!-- App -->
        <script>

            System.config({
                packages: {
                    app: {
                        format: 'register',
                        defaultExtension: 'js'
                    }/*,
                    'node_modules': {
                        format: 'cjs',
                        defaultExtension: 'js'
                    }*/
                }
            });

            System.import('app/main').then(null, console.error.bind(console));

        </script>

    </head>
    <body>
        <my-app></my-app>
    </body>
</html>

我正在使用Angular2 2.0.0-beta.9这是我看到的行为。请注意,@ RouteConfig中的2个路径都没有标记为useAsDefault: true

当我尝试打开http://localhost:8080/#/test1时,页面打开正常,但是当我单击TestComponent模板中的2个锚点之一时,标签会被删除。

然后,如果我将path1设置为useAsDefault: true,即使我尝试访问http://localhost:8080/#/test1,哈希标签也会立即被删除。

有人可以告诉我,如果我做错了或者这是一个错误吗?我只想在URL中找回主题标签。

2 个答案:

答案 0 :(得分:6)

Angular2&lt; = RC.5

update orders set total_items = (SELECT L_QUANTITY AS "TOTAL" FROM (SELECT o_orderkey, SUM(L_Quantity) AS "TOTAL" FROM LINEITEM L JOIN ORDERS O ON O.O_ORDERKEY = L.L_orderkey WHERE L.L_orderkey > 1 GROUP BY o_orderkey)); 需要在ROUTER_PROVIDERS之前添加,否则您之前添加的LocationStrategy会被覆盖。

LocationStrategy

删除此行

bootstrap(
    TestComponent, 
    [
        ROUTER_PROVIDERS, 
        // must be listed after `ROUTER_PROVIDERS`
        provide(LocationStrategy, { useClass: HashLocationStrategy })
    ]
);

来自providers: [ROUTER_PROVIDERS]

答案 1 :(得分:0)

通过实施,一切看起来都很完美。
现在,请尝试删除 browser's cache memory 并尝试删除。我希望它能开始工作!!!
在尝试解决问题时我遇到了这个问题。