简体   繁体   English

从路由emberjs中删除动态路段信息

[英]Remove dynamic segment information from route emberjs

I have an ember engine, and within that I have defined a route. 我有一个余烬引擎,并在其中定义了一条路线。 It works as expected. 它按预期工作。 It's called my-route . 这就是my-route It was defined like this: 定义如下:

this.route('my-route', {path: '/my-route/:myparams'}); in the routes.js file. routes.js文件中。

As you can see, it has a dynamic segment, and shows different values based on myparams . 如您所见,它具有动态段,并根据myparams显示不同的值。

I want to add a nested route to this route . 我想一个嵌套添加route到该route Let's say .../my-route/1 shows a list of items. 假设.../my-route/1显示项目列表。 When the user clicks on any of the items listed on the page, the route should be: my-route/display but instead its my-route/1/display . 当用户单击页面上列出的任何项目时,路由应为: my-route/display而不是其my-route/1/display I don't want the 1 here as it could be misleading. 我不希望此处出现1 ,因为这可能会误导您。

Also, the link-to doesn't open anything either, the click does nothing. 此外, link-to也不会打开任何内容,单击也不会执行任何操作。 This is how I changed my routes.js file: 这就是我更改routes.js文件的方式:

this.route('my-route', {path: '/my-route/:myparams'}, function() { this.route('display'); });

In display.hbs file I added dummy data, and display.js is also empty, just extending from Ember.route . display.hbs文件中,我添加了伪数据,而display.js也为空,只是从Ember.route扩展而来。 The my-route.hbs links like this: my-route.hbs链接如下:

{{#link-to 'my-route.display'}} Open me {{/link-to}}'

I am new to EmberJS, and would appreciate if someone could please tell me how to: 我是EmberJS的新手,如果有人可以告诉我如何操作,我们将不胜感激:

  1. Remove the dynamic segment information 删除动态细分信息
  2. Make the link-to work 使link-to生效

Thank you! 谢谢!

1) I can think of no easy way to remove dynamic segment from the url -- if you must, you can probably just not use dynamic segment and send the information to the transitioning route via other ways - you can probably set the controller directly "newRoute.controller.set('someProperty', my_param);" 1)我认为没有简单的方法可以从网址中删除动态分段-如果必须的话,您可能不使用动态分段,而是通过其他方式将信息发送到过渡路线-您可以直接设置控制器“ newRoute.controller.set('someProperty',my_param);“ or use needs api ( http://emberjs.com/guides/controllers/dependencies-between-controllers/ ) 或使用需求api( http://emberjs.com/guides/controllers/dependencies-between-controllers/

but note that both these methods would be making use of controllers. 但请注意,这两种方法都将使用控制器。 If you need to load data in the route depending on the query params, what comes to mind is using a service 如果您需要根据查询参数在路由中加载数据,那么想到的是使用服务

But these methods are not very sophisticated and I'd say only go for it if dynamic segment is an absolute no. 但是这些方法不是很复杂,我只能说如果动态分段绝对不是,那就去做。

2) this one is easier -- you need to pass in the dynamic part along with the route name when using link to -- as given in ember guide https://guides.emberjs.com/v2.0.0/templates/links/ 2)这很容易-如使用链接指南时需要传递动态部分以及路线名称-如余烬指南https://guides.emberjs.com/v2.0.0/templates/links/

Based on the discussions with @Lux and @Chhirag Kataria, I ended up making two separate routes. 基于与@Lux和@Chhirag Kataria的讨论,我最终做了两条单独的路线。 I used the my-route as a dummy route, and nested the display and the list routes in it. 我将my-route用作虚拟路由,并将displaylist路由嵌套在其中。

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

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