简体   繁体   English

路由 angular 5 ,在 url 后在 params 上传递一个字符串

[英]Routing angular 5 , passing a string on params after url

I am developping an angular project based on geographic system information.我正在开发一个基于地理系统信息的角度项目。 The idea is: I have a component wich its route is : {path: 'home'} I want to pass a geojson url with this route to be like that : {path: 'home/:url'} And in the Onit function of the component i get my url and use it in a specific function.这个想法是:我有一个组件,它的路由是: {path: 'home'}我想通过这个路由传递一个 geojson url 是这样的: {path: 'home/:url'}并且在 Onit 函数中我获取我的 url 并在特定函数中使用它的组件。 But the problem that i meet is when i put string i can get it but a long url it redirect me to the login page.但我遇到的问题是,当我输入字符串时,我可以得到它,但是一个长 url 它将我重定向到登录页面。 My code is:我的代码是:

ngOnInit() {
    this.subscription = this.route.params.subscribe(params => {
        this.load(params['url']);
    });
}

load (url) {
    // code of the function here
}

Thank you in advance.先感谢您。

The issue with this implementation is that you are essentially appending your application url with another url.此实现的问题在于,您实际上是将应用程序url 附加到另一个 url。 If your routes are defined as /home/:url , you can imagine how confused Angular Router will get if you pass in a geojson URL and your application url suddenly looks like /home/http://geojson.io/#map=13/40.7159/-74.0084 - Router can't map that url to any defined route and will just navigate to your default/wildcard route (in your case, /login ).如果你的路由被定义为/home/:url ,你可以想象如果你传入一个 geojson URL 并且你的应用程序 url 突然看起来像/home/http://geojson.io/#map=13/40.7159/-74.0084 Router 会变得多么困惑/home/http://geojson.io/#map=13/40.7159/-74.0084 - 路由器无法将该 url 映射到任何定义的路由,只会导航到您的默认/通配符路由(在您的情况下, /login )。

A good way to pass long strings around is to set the url as a queryParam instead of a route param -- https://alligator.io/angular/query-parameters/ .一个好方法是传递长字符串是设置URL作为queryParam而不是路由参数:param - https://alligator.io/angular/query-parameters/

I suggest removing the /home/:url route and giving something like this a try:我建议删除/home/:url路由并尝试这样的事情:

// in your template
<a [routerLink]="['/home']"
   [queryParams]="{ url: 'http://geojson.io/#map=13/40.7159/-74.0084' }">

// in your component
constructor(
  private route: ActivatedRoute
) {}

ngOnInit() {
  let url = this.route.snapshot.queryParams.url;
  this.load(url);
}

private load(url: string) {
  // do something with the url...
}

To find out, where is the problem, log your route to console and see if it accepts any of your routing.要找出问题出在哪里,请将您的route记录到console并查看它是否接受您的任何路由。 It is common a problem.这是一个常见的问题。 Your route doesn't match any and as you have somewhere in your code something like this in your routing module:您的route与任何route都不匹配,因为您在代码中的某个地方在您的路由模块中是这样的:

{ path: '**', redirectTo: 'login' }

you'll get redirected without any errors at all.您将被重定向而不会出现任何错误。 Remove line like this (for testing purposes) and you'll get an error which tells you, what's exactly wrong.删除这样的行(出于测试目的),你会得到一个错误,告诉你到底出了什么问题。

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

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