如果您对Angular4:具有未定义数量的参数的路由规则和angular定义变量感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Angular4:具有未定义数量的参数的路由规则的各种细节,并对a
如果您对Angular 4:具有未定义数量的参数的路由规则和angular定义变量感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Angular 4:具有未定义数量的参数的路由规则的各种细节,并对angular定义变量进行深入的分析,此外还有关于Angular 2:’…’的路由生成器未包含在传递的参数中、Angular 5中的参数化路由、Angular UI路由器无法解析注入的参数、angular 学习笔记之Angular4.x中的路由的实用技巧。
本文目录一览:- Angular 4:具有未定义数量的参数的路由规则(angular定义变量)
- Angular 2:’…’的路由生成器未包含在传递的参数中
- Angular 5中的参数化路由
- Angular UI路由器无法解析注入的参数
- angular 学习笔记之Angular4.x中的路由
Angular 4:具有未定义数量的参数的路由规则(angular定义变量)
期望的行为:
我能够在面包屑中显示当前路径.
我想用我的路由规则绑定我的痕迹,如下所示:
问题是可能存在无限数量的嵌套文件夹,因此我无法使用静态路由规则.
如何创建这样的规则,我可以检索未定义数量的参数以获取当前文件路径?
我希望能够直接使用此url启动应用程序:localhost:4200 / files / images / myfolder / test /并检索路径,以便我可以直接获取此特定文件夹.
我做了什么:
我只对文件页面有这个简单的路由规则:
const filesRoutes: Routes = [ { path: 'files',component: FilePageComponent } // I need something like that { path: 'files/:folder1/:folder2/[...]',component: FilePageComponent } ];
我当前文件夹的路径只是一个文件数组:this.stackFolder:Array< MyFile>.这就是我获取面包屑内容的方式.
我已经阅读了有关路由的整个angular.io文档,还阅读了有关添加动态路由规则的一些问题.
解决方法
Plunker Example
我认为最好的方法是使用LazyLoading.
>它使您可以进行模块的递归加载.就像文件夹三
>其次,您只能为所有文件夹视图实现一个组件
>这种方法的性能更好,因为您的应用程序不需要在死记硬背之前加载所有模块.
>您可以使用路由参数:id来创建文件夹的正确链接.
看一个例子我希望这可以帮到你
附:我用了一个例子,一个孩子< router-outlet>但我认为你不需要这样做.我只是举例说.
Angular 2:’…’的路由生成器未包含在传递的参数中
authentication.service.ts:
redirectUser(username:string):void { // Redirect user to profile on successful login this.router.navigate(['../Profile/Feed',{username: username}]); }
这一切都很好,但自从我在ProfileComponent中引入了一些子路由以来,我遇到了一些错误.
首先,这是我的AppComponent与RouteConfig:
app.ts:
import {Component,ViewEncapsulation} from 'angular2/core'; import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router'; import {HTTP_PROVIDERS} from 'angular2/http'; import {HomeComponent} from '../home/home'; import {AuthenticationService} from '../../services/authentication.service'; import {LoginComponent} from '../login/login'; import {ProfileComponent} from '../profile/profile'; import {ProfileService} from '../../services/profile.service'; @Component({ selector: 'app',viewProviders: [AuthenticationService,ProfileService,HTTP_PROVIDERS],encapsulation: ViewEncapsulation.None,directives: [ ROUTER_DIRECTIVES ],templateUrl: './components/app/app.html' }) @RouteConfig([ {path: '/',component: HomeComponent,as: 'Home'},{path: '/inloggen',component: LoginComponent,as: 'Login'},{path: '/profile/:username/...',component: ProfileComponent,as: 'Profile'} ]) export class AppComponent { ... }
如您所见,在ProfileComponent中定义了一些新路由.如下图所示:
ProfileComponent.ts:
import {Component,OnInit} from 'angular2/core'; import {RouteConfig,ROUTER_DIRECTIVES,RouteParams,RouterLink,RouterOutlet} from 'angular2/router'; import {ProfileService} from '../../services/profile.service'; import {PROFILE_IMAGES} from '../../constants/constants'; import {WorkoutsComponent} from '../workouts/workouts'; import {FeedComponent} from '../Feed/Feed'; interface IProfileVM { username: string; profileUser: Object; getProfileData(username:string): void; } @Component({ selector: 'profile',templateUrl: './components/profile/profile.html',directives: [RouterOutlet,RouterLink],providers: [ProfileService] }) @RouteConfig([ {path: '/nieuwsFeed',component: FeedComponent,as: 'Feed'},{path: '/workouts',component: WorkoutsComponent,as: 'Workouts'} ]) export class ProfileComponent implements OnInit,IProfileVM { public username:string; public profileUser:any; constructor(private _profileService:ProfileService,private _params:RouteParams) { this.username = this._params.get('username'); } ngOnInit() { this.getProfileData(this.username); } getProfileData(username) { // Do stuff.. } }
所以这里的问题是,在我在ProfileComponent中引入一些子路由之前,一切正常.用户被重定向,用户名出现在URL中,一切正常.但是因为我添加了这些子路由,我收到以下错误:
“用户名’的路由生成器未包含在传递的参数中.”
很高兴有人可以帮助我!提前致谢!
解决方法
因此父组件路由将丢失:配置文件路由中的用户名,如下所示:
@RouteConfig([ {path: '/',{path: '/profile/...',as: 'Profile'} ])
相反,您的配置文件组件将定义:username route参数:
@RouteConfig([ {path: '/:username/nieuwsFeed',{path: '/:username/workouts',as: 'Workouts'} ])
这种方法看起来更直观,并且可以减少在嵌套路由组件之间导航的问题.
Angular 5中的参数化路由
在我的应用程序中我已经路由:
const routes: Routes = [ { path: 'blog/:id',component: BlogComponent },{ path: 'blog/moo',component: MooComponent },];
并且有以下信息:
If we visited /blog/moo we would show MooComponent even though it
matches the path for the first blog/:id route as well.Important Non-parameterised routes take precedence over parameterised
routes.
不幸的是,它并不像那样.如果我将转到url blog / moo,那么路由会将moo视为Id并将打开BlogComponent.
我想知道如何解决我有两个想法:
>我可以改变路径.博客/身份证和博客/ moo.
>我可以使用UrlMatcher但是当我更改previos url段时可能会有些问题.所以这个功能应该做好充分准备.
你怎么看?有任何想法吗?
解决方法
路由器选择具有第一个匹配获胜策略的路由.
>
The router selects the route with a first match wins strategy.
Wildcard routes are the least specific routes in the route
configuration. Be sure it is the last route in the configuration.
>
The order of the routes in the configuration matters and this is by
design. The router uses a first-match wins strategy when matching
routes,so more specific routes should be placed above less specific
routes. In the configuration above,routes with a static path are
listed first,followed by an empty path route,that matches the
default route. The wildcard route comes last because it matches every
URL and should be selected only if no other routes are matched first.
Even this explains the same regarding the route prioritization
所以你应该按照相反的顺序保持你的路线:
const routes: Routes = [ { path: 'blog/moo',{ path: 'blog/:id',];
我想强调上述陈述中的一点:
注意:
更具体的路线应放在不太具体的路线上方.
由于’blog / moo’更具体,您需要将其放在’blog /:id’上方
I have created a PLUNKER for your example
Angular UI路由器无法解析注入的参数
因此,请考虑我的angularUI路由设置中的以下片段。我导航到路线/ category / manage / 4 /
details(例如)。我希望可以在相关的控制器加载之前解决“类别”问题,实际上是可以在我可以从类别服务返回类别的resolve函数中放置一个断点,并查看类别是否已返回的事实。现在在控制器本身内部放置另一个断点,我可以看到“类别”始终是未定义的。它不是由UI路由器注入的。
谁能看到这个问题?它可能不在我提供的代码中,但在运行代码时没有错误,因此无法确定问题的根源在哪里。典型的js静默失败!
.state('category.manage',{
url: '/manage',templateUrl: '/main/category/tree',controller: 'CategoryCtrl'
})
.state('category.manage.view',{
abstract: true,url: '/{categoryId:[0-9]*}',resolve: {
category: ['CategoryService','$stateParams',function (CategoryService,$stateParams) {
return CategoryService.getCategory($stateParams.categoryId).then(returnData); //this line runs before the controller is instantiated
}]
},views: {
'category-content': {
templateUrl: '/main/category/ribbon',controller: ['$scope','category',function ($scope,category) {
$scope.category = category; //category is always undefined,i.e.,UI router is not injecting it
}]
}
},})
.state('category.manage.view.details',{
url: '/details',data: { mode: 'view' },templateUrl: '/main/category/details',controller: 'CategoryDetailsCtrl as details'
})
angular 学习笔记之Angular4.x中的路由
根据不同的地址加载不同组件,实现单页面应用
一、Angular命令创建一个配置好路由的项目
1. 命令创建项目 注意是项目
ng new demo02 –-routing
2. 创建需要的组件
ng g component home
ng g component news
ng g component newscontent
3. 找到app-routing.module.ts 配置路由
引入组件
import { HomeComponent } from ''./home/home.component'';
import { NewsComponent } from ''./news/news.component'';
import { NewscontentComponent } from ''./newscontent/newscontent.component'';
配置路由
const routes: Routes = [
{path: ''home'', component: HomeComponent},
{path: ''news'', component: NewsComponent},
{path: ''newscontent'', component: NewscontentComponent},
{
path: '''',/*链接为空*/
redirectTo: ''/home'',/*默认跳转的路由*/
pathMatch: ''full'' },
//匹配不到路由的时候加载的组件 或者跳转的路由
{ path: ''**'', /*任意的路由*/
// component:HomeComponent
redirectTo:''home'' }
];
4. 找到app.component.html根组件模板,配置router-outlet显示动态加载的路由
<h1>
<a routerLink="/home">首页</a>
<a routerLink="/news">新闻</a>
</h1>
<router-outlet></router-outlet> /*动态加载组件跳转的地方*/
二、Angula4.x在已有的项目中配置路由(上面是通过命令 现在是在建好的项目中配置 看看那些地方不同)
1. 新建组件
ng g component home
ng g component news
ng g component newscontent
2. 新建app-routing.module.ts ,app-routing.module.ts中引入模块
import { NgModule } from ''@angular/core'';
import { Routes, RouterModule } from ''@angular/router'';
3. app-routing.module.ts中引入组件
import { HomeComponent } from ''./home/home.component'';
import { NewsComponent } from ''./news/news.component'';
import { NewscontentComponent } from ''./newscontent/newscontent.component'';
3. app-routing.module.ts中配置组件
const routes: Routes = [ {path: ''home'', component: HomeComponent}, {path: ''news'', component: NewsComponent}, {path: ''newscontent/:id'', component: NewscontentComponent}, { path: '''', redirectTo: ''/home'', pathMatch: ''full'' } ];
4. app-routing.module.ts中配置模块 暴露模块
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
注:当然前面的4步可以通过命令生成的app-routing.module.ts 直接copy到app目录下,这个已经都配置好了
5. 在app.module.ts 引入刚才定义的路由
import { AppRoutingModule } from ''./app-routing.module'';
6.6.app.module.ts里面的import注册这个路由模块
imports: [ BrowserModule, AppRoutingModule ]
7.找到app.component.html根组件模板,配置router-outlet显示动态加载的路由
<h1>
<a routerLink="/home">首页</a>
<a routerLink="/news">新闻</a>
</h1>
<router-outlet></router-outlet>
四、Angular routerLinkActive设置routerLink默认选中路由的class 切换改变样式
<h1>
<a routerLink="/home" routerLinkActive="active">首页</a>
<a routerLink="/news" routerLinkActive="active">新闻</a>
</h1>
.active{ color:red; } //style.css
五、路由的动态传值
1.配置动态路由
<a routerLink="/newsconten/id" routerLinkActive="active">新闻</a>
const routes: Routes = [
{path: ''home'', component: HomeComponent},
{path: ''news'', component: NewsComponent},
{path: ''newscontent/:id'', component: NewscontentComponent}, /*通过/:id这种方式*/
{ path: '''',
redirectTo: ''/home'',
pathMatch: ''full'' }
];
2.获取动态路由的值(组件的ts文件)
import { Router, ActivatedRoute, Params } from ''@angular/router'';
constructor( private route: ActivatedRoute) {
}
ngOnInit() {
console.log(this.route.params);
// this.route.params.subscribe(data=>this.id=data.id);
}
六、路由的js跳转
1. 引入
import { Router } from ''@angular/router'';
2.初始化
export class HomeComponent implements OnInit {
constructor(private router: Router) {
}
ngOnInit() {
}
goNews(){
// this.router.navigate([''/news'', hero.id]);
this.router.navigate([''/news'']);
}
}
3.路由跳转
this.router.navigate([''/news'', hero.id]);
七、路由的js跳转 get传值
1. 引入NavigationExtras
import { Router ,NavigationExtras,ActivatedRoute} from ''@angular/router'';
2.定义一个goNewsContent方法执行跳转,用NavigationExtras配置传参。
goNewsContent(){
let navigationExtras: NavigationExtras = {
queryParams: { ''session_id'': ''123'' },
fragment: ''anchor'' };
this.router.navigate([''/news''],navigationExtras);
}
3.获取get传值
constructor(private route: ActivatedRoute) {
console.log(this.route.queryParams);
}
八、父子路由
1. 创建组件引入组件
import { NewsaddComponent } from ''./components/newsadd/newsadd.component'';
import { NewslistComponent } from ''./components/newslist/newslist.component'';
2. 配置路由
{
path: ''news'', component:NewsComponent,
children: [
{ path:''newslist'', component:NewslistComponent },
{ path:''newsadd'', component:NewsaddComponent }
]
}
3. 父组件中定义router-outlet
我们今天的关于Angular 4:具有未定义数量的参数的路由规则和angular定义变量的分享就到这里,谢谢您的阅读,如果想了解更多关于Angular 2:’…’的路由生成器未包含在传递的参数中、Angular 5中的参数化路由、Angular UI路由器无法解析注入的参数、angular 学习笔记之Angular4.x中的路由的相关信息,可以在本站进行搜索。
本文标签: