gpt4 book ai didi

Angular 2 : what is difference between resolve and data in routing paths?

转载 作者:太空狗 更新时间:2023-10-29 18:16:57 25 4
gpt4 key购买 nike

我看到有两种方法可以将简单数据(例如字符串)从路由路径传递到不同的组件:

第一种方式:

路由端:

export const AppRoutes: Routes = [
...
{
path: '/one', component: OneComponent, resolve: { foo: 'foo' }
}
];

组件端:

@Component()
export class OneComponent implements OnInit {
constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.foo = this.route.snapshot.data['foo'];
}
}

第二种方式:

路由端:

const routes: RouterConfig = [
...
{
path: '/one', component: OneComponent, data : {some_data : 'some value'}
}
];

组件端:

@Component()
export class OneComponent implements OnInit {
constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.obs = this.route
.data
.subscribe(v => console.log(v));
}

ngOnDestroy() {
this.obs.unsubscribe();
}
}

那么向组件传递值的最佳方式是什么? resolvedata 属性之间有什么区别?

最佳答案

data是添加到路由中的静态数据,resolve调用的是一个可以计算数据的服务,同样使用async调用。

您的resolve 示例无效。

参见 https://angular.io/docs/ts/latest/guide/router.html#!#resolve-guard

关于 Angular 2 : what is difference between resolve and data in routing paths?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40174362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com