【问题标题】:Angular2 routing - adding the hashtag using LocationStrategy not working?Angular2 路由 - 使用 LocationStrategy 添加主题标签不起作用?
【发布时间】:2016-06-28 10:09:37
【问题描述】:

我正在关注 Angular2 文档中的简单快速入门应用程序,并且我正在使用 spring 后端来运行它。我的问题是角度路由器从 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 中取回主题标签。

【问题讨论】:

  • 你的路由配置是什么样的?你如何导航到那条路线?
  • P.S.我确实尝试手动添加主题标签,但这破坏了整个应用程序,所以我猜我不打算触摸那里:)
  • 它与 beta-9 中的相同实现工作正常。你有没有在你的应用中做过一些特别的事情?如果有,请告诉我们。
  • 我不这么认为 - 我只是按照 [link]angular.io/docs/ts/latest/quickstart.html[/link] 的快速入门。它也对我有用,但是一旦加载页面,主题标签就会自动删除。它会为您保留在 URL 中吗?
  • 没有服务器端代码和导航(意味着没有服务器交互)尝试运行你的应用程序并检查它是否有效?这意味着只需使用 angular2 和路由检查是否一切都按预期工作。

标签: url angular hashtag angular2-routing


【解决方案1】:

Angular2

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

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

删除这一行

providers: [ROUTER_PROVIDERS]

来自TestComponent

【讨论】:

  • 非常感谢!!!这让我发疯了——既然你指出了这一点,我就明白发生了什么。对其进行了测试,现在一切正常。再次感谢!
  • 很高兴听到!这是人们在开始使用 Angular DI 时遇到的常见陷阱之一。
  • 感谢您的回复,我的应用程序遇到了问题,因为我在组件和引导类中添加了路由器提供程序,因为我从组件中删除了它可以工作!
  • @GünterZöchbauer 您的回答帮助了我(为此困扰了几天)..我确实有一个小问题..为什么我们应该从main-app 组件中删除providers: [ROUTER_PROVIDERS]? &然后进行试用,我制作了 bootstrap(AppComponent, []); 之类的引导程序,然后在 AppComponent 中添加了这些依赖项,例如 providers: [ ROUTER_PROVIDERS, , provide(LocationStrategy, { useClass: HashLocationStrategy }) ]..你知道它为什么不起作用的原因吗..
  • 我没有找到明确的信息,但我自己亲身体验过,路由器只有在将ROUTER_PROVIDERS添加到bootstrap()时才有效
【解决方案2】:

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

【讨论】:

  • 这不起作用 - 感谢您的调查。我总是进行硬刷新(ctr + f5),我也尝试了隐身模式,然后清除了现金,最后我尝试了其他我没有使用过 webapp 的浏览器(所以那里没有缓存)并且仍然是同样的事情。当我打开localhost:8080/#/dashboard 时,页面加载并立即更改为localhost:8080/dashboard。我会继续挖掘,直到我弄明白,一旦我弄明白,我会发布解决方案。再次感谢。
  • 好的没问题。我面对它,所以把它放在这里作为答案,并认为可能会帮助你或其他人......没关系!我认为有些东西会覆盖您的配置或其他东西,但如果不检查您的代码就很难告诉您。我希望你很快就能自己解决它。
猜你喜欢
  • 2017-01-06
  • 1970-01-01
  • 2016-01-18
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 2016-07-11
  • 1970-01-01
相关资源
最近更新 更多