简体   繁体   English

在 Angular 降级组件中访问 $stateParams

[英]Accessing $stateParams within an Angular downgraded component

I'm currently working on a hybrid project Angular/AngularJS, we are using downgradeComponent from Angular and it is working impressively well.我目前正在开发一个混合项目 Angular/AngularJS,我们正在使用 Angular 的downgradeComponent ,它运行得非常好。

Besides it's fairly easy dynamics, sometimes we encounter some obstacles.除了相当容易的动态之外,有时我们会遇到一些障碍。 There is this page that uses an url param for a few features, that was working pretty well in AngularJS, but now, routing to the new Angular version, it's not working at all.有这个页面使用 url 参数来实现一些功能,在 AngularJS 中运行良好,但现在,路由到新的 Angular 版本根本不起作用,

The previous route state, pointing to the old page looked like this:之前的路由 state,指向旧页面看起来是这样的:

.state('userEdit', {
    title: 'routes_userEdit',
    url: '/user/:id',
    template: require('html-loader!./user/userEdit.html').default,
    controller: 'UserEditController'
})

This way, the id parameter could easily be accessible at UserEditCntroller.js using $stateParams.id这样,可以在UserEditCntroller.js上使用$stateParams.id轻松访问id参数

But now, with the new Angular component it looks like this但是现在,有了新的 Angular 组件,它看起来像这样

.state('userEdit', {
    title: 'routes_userEdit',
    url: '/user/:id',
    template: `<ng2-user-edit></ng2-user-edit>`
})

It's similar to the previous code, but the template being an Angular component it can't access AngularJS's $stateParams .它与前面的代码类似,但模板是 Angular 组件,它无法访问 AngularJS 的$stateParams

My question is.. How do I access $stateParams in an Angular2+ downgraded component?我的问题是.. 如何在 Angular2+ 降级组件中访问$stateParams I already tried using Angular's ActivatedRoute component, but it returns an empty array of params.我已经尝试过使用 Angular 的ActivatedRoute组件,但它返回一个空的参数数组。

If Angularjs rules routing, u can access $stateParams as usual ng1 service:如果 Angularjs 规则路由,你可以像往常一样访问$stateParams ng1 服务:

providers: [
    {provide: '$stateParams', useFactory: (i: any) => i.get('$stateParams'), deps ['$injector']}
]

And in component @Inject('$stateParams') $stateParams在组件@Inject('$stateParams') $stateParams

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM