I am beginner at emberjs, I want to access url params in component but don't know how it is done in ember js.
I could get url params in route with code below:
model(params) {
this.store.query('ticket', {
page: {
number: params.page,
size: params.size
}
});
},
queryParams: {
page: {
refreshModel: true
},
size: {
refreshModel: true
}
}
For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.
import ActivatedRoute from '@angular/router'. Inject ActivatedRoute class in constructor. Access queryParams property of ActivatedRoute class which returns an observable of the query parameters that are available in the current URL route.
EmberJS - Query ParametersQuery parameters are specified on route driven controllers which appear to the right of the ? in a URL and are represented as optional key-value pairs. The above URL has the two query parameters; one is sort and the other is page which contains values ASC and 2 respectively.
Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.
The quick answer:
You can inject the router into the component where you need it like this:
import Ember from 'ember';
const { computed: { alias }, observer } = Ember
export default Ember.Component.extend({
routing: Ember.inject.service('-routing'),
params: alias('routing.router.currentState.routerJsState.fullQueryParams')
})
Here is a twiddle with this implemented.
The longer answer:
You're using the injecting routing which is private, and using routerJsState.fullQueryParams like that is not really standard practice.
Because a query param is really a property somewhere, if you can make that "somewhere" a service, it would be better, because then you can inject that service into your component instead of routing like we do now. For example page and limit can live on a service and be injected into a controller and component, and would be a nicer solution.
Here is that solution in a twiddle.
The relevant code:
services/task-pager.js
import Ember from 'ember';
export default Ember.Service.extend({
page: 0,
limit: 3,
nextPage: function() {
this.set('page',this.get('page') + 1)
console.log(this.get('page'))
},
previousPage: function() {
this.set('page',this.get('page') - 1)
}
})
// controllers/tasks.js
export default Ember.Controller.extend({
taskPager: Ember.inject.service(),
queryParams:['page', 'limit'],
page: Ember.computed.alias('taskPager.page'),
limit: Ember.computed.alias('taskPager.limit'),
actions: {
nextPage: function() {
this.get('taskPager').nextPage()
},
previousPage: function() {
this.get('taskPager').previousPage()
}
}
})
//components/display-qp.js
export default Ember.Component.extend({
params: Ember.inject.service('task-pager')
})
//templates/components/display-qp.hbs
COMPONENT PARAMS PAGE:{{params.page}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With