加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

刷新页面时angular2 rc1默认路由问题

发布时间:2020-12-17 06:54:43 所属栏目:安全 来源:网络整理
导读:在RC1的新路由器中,无法再使用useAsDefault.相反,现在默认路由在app.component.ts中实现 ngOnInit() { this.router.navigate(['/login']); } 如果我通过按浏览器上的“重新加载”按钮刷新页面,那么我当前的页面URL(例如,http:// localhost:3000 / heroshel
在RC1的新路由器中,无法再使用useAsDefault.相反,现在默认路由在app.component.ts中实现

ngOnInit() {
    this.router.navigate(['/login']);
  }

如果我通过按浏览器上的“重新加载”按钮刷新页面,那么我当前的页面URL(例如,http:// localhost:3000 / heroshell / heroes)将更改为http:// localhost:3000 / login,因为每次我点击重新加载按钮,它将通过app.component.ts

ngOnInit() {
    this.router.navigate(['/login']);
  }

我的当前页面仍会显示,但出现错误.

browser_adapter.ts:78 EXCEPTION: Error in app/apps/hero/hero-shell.component.html:3:7
browser_adapter.ts:78 ORIGINAL EXCEPTION: TypeError: Cannot read property 'map' of null

这是app / apps / hero / hero-shell.component.html

<my-app1>
<h1>{{title}}</h1>
<nav>
    <a [routerLink]="['dashboard']">Dashboard</a>
    <a [routerLink]="['heroes']">Heroes</a>
</nav>
<router-outlet></router-outlet>
</my-app1>

所以我的问题是

>什么是“属性’映射’为null”?
>有没有办法在不通过ngOnInit()的情况下制作默认路线?
>或者如何在页面重新加载期间解决此URL和页面内容不一致的问题?

Beta路由没有这种行为,因为不需要通过ngOnInit()来定义默认路由.

有关我的文件夹结构和路由设置的更多信息,请参阅Angular2 RC1 child routes defined but not recognized

更新:
如果我使用

{ path: '/',component: Login },

配对

ngOnInit() {
    this.router.navigate(['/']);
  }

然后,当重新加载按钮被击中并且错误保持不变时,URL将更改为http:// localhost:3000 /.

同样,如果我使用上面的更新将路径更改为”,那么当重新加载按钮被点击时,URL将被更改为’http://localhost:3000/‘并且错误保持不变.

解决问题的关键是使用两件事的组合:

>在app.component.ts中的@Routes中使用’/’或’*’定义根路径

{path:’*’,component:Login},
>在app.component.ts中删除ngOnInit(),以便它不会更改URL.

再次感谢大家的投入.干杯:)

解决方法

对不起,不明白第一和第三个问题,但是为此:

  1. Is there any way to make a default route without going through ngOnInit()?

你试过“*”作为默认路线吗?

{ path: '/heroshell',component: HeroShellComponent },{ path: '/home',component: HomeComponent },{ path: '/login',{ path: '*',

默认路由将是“登录”,“/ login”路径也将起作用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读