是否可以在Angular 2路线中有一个可选的路线参数?我在RouteConfig中尝试了Angular 1.x语法但收到了以下错误:
"ORIGINAL EXCEPTION:Path"/ user /:id?"包含"?",这在路由配置中是不允许的."
@RouteConfig([
{
path: '/user/:id?',
component: User,
as: 'User'
}])
Run Code Online (Sandbox Code Playgroud) 我有一个父组件进入服务器并获取一个对象.
// parent component
@Component({
selector : 'node-display',
template : `
<router-outlet [node]="node"></router-outlet>
`
})
export class NodeDisplayComponent implements OnInit {
node: Node;
ngOnInit(): void {
this.nodeService.getNode(path)
.subscribe(
node => {
this.node = node;
},
err => {
console.log(err);
}
);
}
Run Code Online (Sandbox Code Playgroud)
在(孩子中的一个)我的孩子展示
export class ChildDisplay implements OnInit{
@Input()
node: Node;
ngOnInit(): void {
console.log(this.node);
}
}
Run Code Online (Sandbox Code Playgroud)
看起来我没想到只能将数据注入路由器插座.看起来我在Web控制台中收到了错误...
Can't bind to 'node' since it isn't a known property of 'router-outlet'.
Run Code Online (Sandbox Code Playgroud)
这有点道理,但我怎么做以下......
// parent component
@Component({
selector : 'node-display',
template : …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,一些URL采用该表单
/department/:dep/employee/:emp/contacts
Run Code Online (Sandbox Code Playgroud)
在我的侧边栏中,我显示了所有员工的列表,每个员工都有一个[routerLink]链接到该员工联系人的员工
<ul>
<li>
<a [routerLink]="['/department', 1, 'employee', 1, 'contacts']"></a>
<li>
<li>
<a [routerLink]="['/department', 1, 'employee', 2, 'contacts']"></a>
<li>
...
</ul>
Run Code Online (Sandbox Code Playgroud)
然而,通过这种方法,我总是需要提供完整的路径,包括所有的参数(dep如上例所示),这非常麻烦.有没有办法提供路线的一部分,例如
<a [routerLink]="['employee', 2, 'contacts']"></a>
Run Code Online (Sandbox Code Playgroud)
(没有那个department部分,因为我并不真正关心department那个观点,而我的感觉是,这反对分离了关注点)?
我有一个组件,我选择一组图像对象.我想将这些选定的图像传递给我的新路径CreateAlbum.它嵌套的数据,不适合作为URL参数传递.
有没有简单的方法来实现这一目标?
这是我导航到路线的代码
public gotoCreateAlbum(): void {
this.router.navigate([('/create-album')])
}
Run Code Online (Sandbox Code Playgroud)
我选择的数据位于此变量中
@Input() selectedPhotos: IPhoto[];
Run Code Online (Sandbox Code Playgroud)
这是我的路由模块
const routes: Routes = [
{ path: 'photos', component: PhotosComponent},
{ path: 'photos/:filter', component: PhotosComponent},
{ path: 'create-album', component: CreateAlbumComponent}
];
Run Code Online (Sandbox Code Playgroud)
基本上我想执行相同的操作,就像CreateAlbum组件是我当前组件的子组件一样,在这种情况下我会使用@Input()
在最新版本的Angular 7.2.6中,我尝试在路由器本身中传递数据
this.router.navigate(['other'], {state: {someData: 'qwert'}}
Run Code Online (Sandbox Code Playgroud)
在OtherComponent文件中,this.router.getCurrentNavigation()始终返回null
在Angular 4中搜索关于各种类型的路由的多个线程/问题之后,我无法解决将queryParams传递给Angular 4路由的问题.
传递到网址时
http://localhost/search;x=y
Run Code Online (Sandbox Code Playgroud)
通过模板[queryParams] = {x:'y'}
<a [routerLink]="/search" [queryParams]="{x: 'y'}">Navigate</a>
Run Code Online (Sandbox Code Playgroud)
或在组件类中
this._router.navigate(['/search'], { queryParams: {x: 'y'} });
Run Code Online (Sandbox Code Playgroud)
结果是路由器抛出匹配错误:
Error: Cannot match any routes. URL Segment: 'search%3Fparam1%3Dtest1%26param2%3Dtest2'
Run Code Online (Sandbox Code Playgroud)
当将enableTracing设置为true时,我可以看到导航对可疑字符进行编码,这很可能是它无法匹配的原因.
我需要处理包含queryParams的URL并解析它们以进行api调用,因此必须在必需或可选的参数上使用查询参数路由.
有没有人有类似的问题,如果有的话,编码根源(ahem.)问题的原因是什么?
这个问题仅在升级到 Angular 15 后出现,我将一个状态属性传递给路由器,但 this.router.getCurrentNavigation() 返回一个空值,该值已router.navigate在另一个组件中传递
A组份:
组分B:
我可以使用组件 B 访问状态,this.state = history.state;但this.state = this.router.getCurrentNavigation()?.extras?.state ?? {};不起作用,它说 getCurrentNavigation() 返回 null。那是什么?
我知道很多人已经问过这个问题,但是没有一个人给出明确的答案。我只想在导航到另一个然后返回到上一个组件时存储组件的状态。我知道我们可以将数据保存在服务中,然后将数据提取回组件。但是,我认为这很容易,但是当组件过于复杂时,并不是更好的解决方案。我读了一些路由器重用的东西,但是它们都不在角度2中工作。
我试图通过点击一个图标导航到一个新页面,下面是代码
this.prj = e.data.project_number;
this.router.navigateByUrl('/dashboard/ProjectShipment/634');
Run Code Online (Sandbox Code Playgroud)
而不是这个硬编码的查询参数,000634我必须将 a 传递this.prj给它。我的路径如下
const appRoutes: Routes = [
{
path: 'dB',
data: { title: 'Dashboard' },
children: [
{
path: 'ProjectShipment/:reportProject',
component: ProjectShipmentComponent,
data: { title: 'Project Shipment' },
}
Run Code Online (Sandbox Code Playgroud) 我有 2 个 Web 应用程序A和B。Web 应用程序 B 是 Angular 6 的基础。
在 Web 应用程序A 中,它获得了一个链接,该链接使用查询字符串引用 B,例如http://B/receipt?referencenumber=11111&title=test&description=sdfsd。
在 Web 应用程序B 中,我定义了如下路由。
{ path: 'receipt/:referencenumber/:title/:description', component: ReceiptComponent},
{ path: '', component: HomeComponent, pathMatch: 'full'},
{ path: 'home', component: HomeComponent},
{ path: 'error', component: ErrorComponent },
{ path: '**', component: ErrorComponent, data: { error: 404 } }
Run Code Online (Sandbox Code Playgroud)
问题:
如果 Web 应用程序A,使用链接http://B/receipt?referencenumber=11111&title=test&description=sdfsd,它将被重定向到 Web 应用程序 B 中的 404 错误页面。
有人在这里有想法吗?我尝试将 …
我使用带有可选参数的角度。我通过导航路由到一个位置:
router.navigate(["route1"], { p1: v1 });
Run Code Online (Sandbox Code Playgroud)
我看到了位置
/路线1;p1=v1
现在我尝试导航到
/路线1;p1=v1 ;p2=v2
我搜索类似的东西
router.navigate({ p2: v2 }, relative);
Run Code Online (Sandbox Code Playgroud)
但我在寻找有效的语法时遇到问题。
我正在使用此代码来重定向this.router.navigate(['/abc']);。如何使用在新页面上显示一些消息<ngb-alert>,是否可以在重定向时传递一些变量但不能作为 url 查询参数?
我试图在我的路线中获取一个参数,但是当我尝试打印它时,在我的控制台中返回 undefined 。
这是我的路由模块:
const routes: Routes = [
{
path: ':id',
component: ListaDadosPvSelecionadoComponent,
canActivate: [AuthGuard],
},
];
Run Code Online (Sandbox Code Playgroud)
这就是我试图获取参数的方式:
ngOnInit() {
this.activatedRoute.queryParams.subscribe((params) => console.log(params['id']));
}
Run Code Online (Sandbox Code Playgroud)
当我去route/3我的组件被渲染但我得到:
不明确的
为什么?
angular ×13
typescript ×3
javascript ×2
angular6 ×1
angular7 ×1
router ×1
routes ×1
routing ×1
url-encoding ×1