【问题标题】:Angular 2: url change in the browser but the view is not displayedAngular 2:浏览器中的网址更改但未显示视图
【发布时间】:2016-03-19 06:13:12
【问题描述】:

我有一个简单的应用程序我在尝试导航到路线时遇到问题。 我的主要组件:

    @Component({
    selector: 'my-app',
    template: `
    <ul class="my-app">
        <li>
            <a [routerLink]="['Login']">Login</a>    
        </li>
        <li>
            <a [routerLink]="['Projects']">Projects</a>  
        </li>
    </ul> 
     <br>
    <router-outlet></router-outlet>`,
    directives: [ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})
@RouteConfig([
    {
        path: '/Login',
        name: 'Login',
        component: LoginFormComponent,
        useAsDefault: false
    },
    {
        path: '/Projects',
        name: 'Projects',
        component: ListProjectsComponent,
        useAsDefault: true
    }
 ])

    export class AppComponent { ...
    ...

我有一个基本组件

@Component({
    selector: 'login-form',
    template: 
      `
        <button (click)="goToProjects()">Go To Projects!</button>
      `,
    directives: [ROUTER_DIRECTIVES],
    providers: [LoginService, ROUTER_PROVIDERS]
})

export class LoginFormComponent implements OnInit {
...
 goToProjects(){
         let link = ['Projects',{}];
         this.router.navigate(link);
    }
..
}

我将基本 Href 添加到主页“index.html”

<head>
    <base href="/">
  </head>

当我点击按钮时,地址栏中的 url 改变了,但视图没有显示,可能是什么问题?

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    我“偶然”发现这个Answer 是为了另一个问题

    bootstrap(
        TestComponent, 
        [
            ROUTER_PROVIDERS, 
            // must be listed after `ROUTER_PROVIDERS`
            provide(LocationStrategy, { useClass: HashLocationStrategy }) // not  interested in this just in the original answer 
        ]
    );
    
    providers: [ROUTER_PROVIDERS] // <-- ** delete this line this is what worked!**
    from LoginComponent
    

    所以,如果 Bootstrap 类已经声明了“ROUTER_PROVIDERS”,那么我需要从每个组件中删除它们,某些操作会默默地失败:导航或 hashbang(如原始答案中所示)以及其他可能的东西..

    【讨论】:

    • 奇怪的是,当我实施这个解决方案时,它似乎可以工作,但现在我所有的页面加载速度都非常慢并且越来越慢。在导航中第三次单击时,我将等待 5-10 秒以等待视图更新。
    • 此 ROUTER_PROVIDERS 已折旧。关于新替代品的任何更新?问题是我的 url 更改但组件没有加载,我也没有引导程序,我有材料。建议?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多