在这篇文章中,我们将为您详细介绍角子路由重定向到父路由而不加载父组件的内容。此外,我们还会涉及一些关于Angular2子路由是否会刷新父路由、Angular4.x通过路由守卫进行路由重定向实现根据条件
在这篇文章中,我们将为您详细介绍角子路由重定向到父路由而不加载父组件的内容。此外,我们还会涉及一些关于Angular 2子路由是否会刷新父路由、Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)、Angular4.x通过路由守卫进行路由重定向步骤详解、Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面的知识,以帮助您更全面地了解这个主题。
本文目录一览:- 角子路由重定向到父路由而不加载父组件
- Angular 2子路由是否会刷新父路由
- Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)
- Angular4.x通过路由守卫进行路由重定向步骤详解
- Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面
角子路由重定向到父路由而不加载父组件
如何解决角子路由重定向到父路由而不加载父组件?
更新: 我只是要编辑组件代码,我本来不想做的,但是出于对时间轴的谨慎,我要使用location.go()方法从URL中删除值
我有一个我正在开发的angular 8应用程序。当路由不完整或格式错误时,我正在尝试使子路由重定向到其父路由。使用redirectTo时,应用程序将正确更新url,但似乎没有再次加载该组件。
下面是我的路由器代码示例。
{
path: ''route1'',component: RouteComponent,},children: [
{
path: '':id/:id2'',component: RouteComponentDetails,{
path: '':id'',redirectTo: ''/route1'',],}
如果我设置了这样的路由,该路由将正确地重新加载,但是它将:id参数留在了我不想要的URL中。我还尝试将path设置为**,这将正确更改路由,使用redirectTo时不会重新加载组件。
{
path: ''route1'',}
有没有办法做到这一点?预先感谢?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Angular 2子路由是否会刷新父路由
就像在视频中播放视频并将路径出口视为该视频的兄弟一样,此页面更改是否会触发视频重新加载?
如果是这样,可以预防吗?
解决方法
Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)
需求:
最近在做一个网上商城的项目,技术用的是Angular4.x。有一个很常见的需求是:用户在点击“我的”按钮时读取cookie,如果有数据,则跳转到个人信息页面,否则跳转到注册或登录页面
解决
在这里通过Angular的路由守卫来实现该功能。
1. 配置路由信息
const routes = [ { path: ''home'', component: HomeComponent }, { path: ''product'', component: ProductComponent }, { path: ''register'', component: RegisterComponent }, { path: ''my'', component: MyComponent }, { path: ''login'', component: LoginComponent, canActivate: [RouteguardService] },//canActivate就是路由守卫 { path: '''', redirectTo: ''/home'', pathMatch: ''full'' } ]
2. 路由守卫条件(RouteguardService.ts)
import { Injectable, Inject } from "@angular/core"; import { DOCUMENT } from "@angular/common"; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, NavigationStart } from "@angular/router"; import userModel from "./user.model"; @Injectable() export class RouteguardService implements CanActivate { constructor(private router: Router, @Inject(DOCUMENT) private document: any) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { // this.setCookie("userId", "18734132326", 10); //读取cookie var cookies = this.document.cookie.split(";"); var userInfo = { userId: "", pw: "" }; if (cookies.length > 0) { for (var cookie of cookies) { if (cookie.indexOf("userId=") > -1) { userModel.accout = cookie.split("=")[0]; userModel.password = cookie.split("=")[1]; userModel.isLogin = false; } } } //获取当前路由配置信息 var path = route.routeConfig.path; if (path == "login") { if (!userModel.isLogin) { //读取cookie如果没有用户信息,则跳转到当前登录页 return true; } else { //如果已经登录了则跳转到个人信息页面,下面语句是通过ts进行路由导航的 this.router.navigate([''product'']) } } } setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + "; " + expires; } }
总结
以上所述是小编给大家介绍的Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
- 对angular4子路由&辅助路由详解
- Angular2之二级路由详解
- 详解Angular路由之路由守卫
- 详解Angular5路由传值方式及其相关问题
- 详解Angular路由 ng-route和ui-router的区别
- 详解AngularJS1.6版本中ui-router路由中/#!/的解决方法
- angular2中router路由跳转navigate的使用与刷新页面问题详解
- Angular 4.x 路由快速入门学习
- 详解Angular路由之子路由
Angular4.x通过路由守卫进行路由重定向步骤详解
这次给大家带来Angular4.x通过路由守卫进行路由重定向步骤详解,Angular4.x通过路由守卫进行路由重定向的注意事项有哪些,下面就是实战案例,一起来看一下。
需求:
最近在做一个网上商城的项目,技术用的是Angular4.x。有一个很常见的需求是:用户在点击“我的”按钮时读取cookie,如果有数据,则跳转到个人信息页面,否则跳转到注册或登录页面
解决
在这里通过Angular的路由守卫来实现该功能。
1. 配置路由信息
const routes = [ { path: ''home'', component: HomeComponent }, { path: ''product'', component: ProductComponent }, { path: ''register'', component: RegisterComponent }, { path: ''my'', component: MyComponent }, { path: ''login'', component: LoginComponent, canActivate: [RouteguardService] },//canActivate就是路由守卫 { path: '''', redirectTo: ''/home'', pathMatch: ''full'' } ]
2. 路由守卫条件(RouteguardService.ts)
import { Injectable, Inject } from "@angular/core"; import { DOCUMENT } from "@angular/common"; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, NavigationStart } from "@angular/router"; import userModel from "./user.model"; @Injectable() export class RouteguardService implements CanActivate { constructor(private router: Router, @Inject(DOCUMENT) private document: any) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { // this.setCookie("userId", "18734132326", 10); //读取cookie var cookies = this.document.cookie.split(";"); var userInfo = { userId: "", pw: "" }; if (cookies.length > 0) { for (var cookie of cookies) { if (cookie.indexOf("userId=") > -1) { userModel.accout = cookie.split("=")[0]; userModel.password = cookie.split("=")[1]; userModel.isLogin = false; } } } //获取当前路由配置信息 var path = route.routeConfig.path; if (path == "login") { if (!userModel.isLogin) { //读取cookie如果没有用户信息,则跳转到当前登录页 return true; } else { //如果已经登录了则跳转到个人信息页面,下面语句是通过ts进行路由导航的 this.router.navigate([''product'']) } } } setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + "; " + expires; } }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
vue计算属性和监听器案例代码分析
js正则相关使用案例分享
以上就是Angular4.x通过路由守卫进行路由重定向步骤详解的详细内容,更多请关注php中文网其它相关文章!
Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面
需求:
最近在做一个网上商城的项目,技术用的是Angular4.x。有一个很常见的需求是:用户在点击“我的”按钮时读取cookie,如果有数据,则跳转到个人信息页面,否则跳转到注册或登录页面
解决
在这里通过Angular的路由守卫来实现该功能。
1. 配置路由信息
const routes = [
{ path: ''home'', component: HomeComponent },
{ path: ''product'', component: ProductComponent },
{ path: ''register'', component: RegisterComponent },
{ path: ''my'', component: MyComponent },
{ path: ''login'', component: LoginComponent, canActivate: [RouteguardService] },//canActivate就是路由守卫
{ path: '''', redirectTo: ''/home'', pathMatch: ''full'' }
]
2. 路由守卫条件(RouteguardService.ts)
import { Injectable, Inject } from "@angular/core";
import { DOCUMENT } from "@angular/common";
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, NavigationStart } from "@angular/router";
import userModel from "./user.model";
@Injectable()
export class RouteguardService implements CanActivate {
constructor(private router: Router, @Inject(DOCUMENT) private document: any) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
// this.setCookie("userId", "123456", 10);
//读取cookie
var cookies = this.document.cookie.split(";");
var userInfo = { userId: "", pw: "" };
if (cookies.length > 0) {
for (var cookie of cookies) {
if (cookie.indexOf("userId=") > -1) {
userModel.accout = cookie.split("=")[0];
userModel.password = cookie.split("=")[1];
userModel.isLogin = false;
}
}
}
//获取当前路由配置信息
var path = route.routeConfig.path;
if (path == "login") {
if (!userModel.isLogin) {
//读取cookie如果没有用户信息,则跳转到当前登录页
return true;
} else {
//如果已经登录了则跳转到个人信息页面,下面语句是通过ts进行路由导航的
this.router.navigate([''product''])
}
}
}
setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
}
关于角子路由重定向到父路由而不加载父组件的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Angular 2子路由是否会刷新父路由、Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)、Angular4.x通过路由守卫进行路由重定向步骤详解、Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面等相关知识的信息别忘了在本站进行查找喔。
本文标签: