簡體   English   中英

如何訪問ember.js中路由文件中的:id參數?

[英]How do I access the :id parameter in the route file in ember.js?

我的Ember應用程式中有這些路線:

Router.map(function() {
  this.resource('posts', function () {
    this.route('show',  {path: '/:id'});
  });
});

讓我們集中於url /posts/:idshow路線。它使用PostsShowController ,如下所示:

import Ember from "ember";
export default Ember.ObjectController.extend({});

路由文件/routes/posts/show.js如下所示:

import Ember from 'ember';

var PostsIndexRoute = Ember.Route.extend({
  model: function() {
    return posts[variable];
  }
});

var posts = ...

在第五行,它說posts[variable]我想用在URL中傳遞的:id參數替換variable 因此,如果我輸入localhost:4200 / posts / 3,我希望它是3。如何執行此操作?

確保您仔細考慮自己的路線。 “ / posts”表示您正在查看所有帖子,而“ / post /:id”表示您正在查看具有給定id的帖子。 因此,您可能想要類似以下內容:

this.resource('posts');
this.resource('post', { path: 'post/:id' });

這將給您兩條路線:

  • /職位
  • /后/:ID

然后,您可以提供相應的Ember Route對象,如下所示:

App.PostsRoute = Ember.Route.extend({
  model: function () {
    // return all posts
  }
});

App.PostRoute = Ember.Route.extend({
  model: function (params) {
    // params.id will be the :id value from the route
    // return post by id
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM