Angular2 路由 - 使用 LocationStrategy 添加主题标签不起作用? [英] Angular2 routing - adding the hashtag using LocationStrategy not working?

查看:27
本文介绍了Angular2 路由 - 使用 LocationStrategy 添加主题标签不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm following the simple quickstart app from the Angular2 docs and I'm using a spring backend to run it. My problem is that the angular router ditched the hashtag from the URL so what should have been example.com/#/dashboard is now example.com/dashboard.

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

I am using the LocationStrategy method specified in a bunch of posts on StackOverflow. Below is my simple example:

文件: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 })
    ]
);

文件: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 { }

文件: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.

I'm using Angular2 2.0.0-beta.9 and this is the behavior that I see. Note that none of the 2 paths in the @RouteConfig are marked with useAsDefault: true.

当我尝试打开 http://localhost:8080/#/test1 页面打开正常,但是当我单击 TestComponent 模板中的 2 个锚点之一时,主题标签获取掉线了.

When I try to open up http://localhost:8080/#/test1 the page opens fine, but when I click on one of the 2 anchors in the TestComponent template, the hashtag gets dropped.

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

Then if I set path1 to be useAsDefault: true, the hashtag gets dropped immediately even when I try to visit http://localhost:8080/#/test1.

有人可以告诉我是我做错了什么还是错误?我只想在 URL 中取回主题标签.

Can someone please tell me if I'm doing something wrong or if that's a bug? I just want to get the hashtag back in the URL.

推荐答案

Angular2 <= RC.5

ROUTER_PROVIDERS 需要在 LocationStrategy 之前添加,否则您之前添加的 LocationStrategy 会被覆盖.

The ROUTER_PROVIDERS need to be added before LocationStrategy otherwise your previously added LocationStrategy gets overridden.

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

删除这一行

providers: [ROUTER_PROVIDERS]

来自 TestComponent

这篇关于Angular2 路由 - 使用 LocationStrategy 添加主题标签不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆