简体   繁体   English

具有特定参数的角度路由匹配 url

[英]Angular routing match url with a specific parameter

I'm working on implementing an angular app inside of the WordPress admin dashboard.我正在致力于在 WordPress 管理仪表板内实现一个角度应用程序。

I'm currently trying to implement routes and I'd like to know if there's a way to match routes with a specific parameter.我目前正在尝试实现路由,我想知道是否有办法将路由与特定参数匹配。 For example, my plugin's base URL in WordPress is:例如,我的插件在 WordPress 中的基本 URL 是:

http://localhost/wp/wp-admin/admin.php?page=angular_plugin

This matches with the root path of angular routing.这与角度路由的根路径相匹配。 However, if I want to implement more pages, I can't use a different path since WordPress is not going to recognize it and will return an error.但是,如果我想实现更多页面,我不能使用不同的路径,因为 WordPress 不会识别它并会返回错误。 So to avoid WordPress messing around with my routes, I need to use the plugin's base URL and play with the parameters to indicate which route I'm trying to load.因此,为了避免 WordPress 弄乱我的路由,我需要使用插件的基本 URL 并使用参数来指示我要加载的路由。

For example, to load the dashboard route, it would have to be something like:例如,要加载仪表板路线,它必须是这样的:

http://localhost/wp/wp-admin/admin.php?page=angular_plugin&route=dashboard

Is there a way to declare routes like this in Angular?有没有办法在 Angular 中声明这样的路由?

You can give hash location strategy a try.您可以尝试使用哈希定位策略。 Then the url would be something like http://localhost/wp/wp-admin/admin.php?page=angular_plugin/#/dashboard this way wordpress won't bother because it is just a thing to be handled in javascript.然后 url 将类似于http://localhost/wp/wp-admin/admin.php?page=angular_plugin/#/dashboard这样 wordpress 不会打扰,因为它只是在 javascript 中处理的事情。

Angular's default location strategy is PathLocationStrategy, so you need to enable HashLocationStrategy which is trivial to do, just make sure you have useHash set as true in your routing module ts file like below Angular 的默认定位策略是 PathLocationStrategy,因此您需要启用 HashLocationStrategy,这很容易做到,只需确保在路由模块 ts 文件中将 useHash 设置为 true,如下所示

@NgModule({
  imports: [ RouterModule.forRoot(routes, {useHash: true }) ],
  exports: [ RouterModule ]
})

If you want to use angular on wordpress admin,you can just point every incoming request to /wp/wp-admin/admin.php/foo/bar to /wp/wp-admin/admin.php with htaccess如果你想在 wordpress admin 上使用 angular,你只需使用 htaccess 将每个传入请求指向/wp/wp-admin/admin.php/foo/bar/wp/wp-admin/admin.php

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

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