【问题标题】:Ember-cli Error while processing route: category.indexEmber-cli 处理路由时出错:category.index
【发布时间】:2014-12-10 10:22:17
【问题描述】:

这是我第一个使用 EmberJS 和 Ember-CLI 的项目。 我运行以下命令:

ember new site  //version: 0.1.2
ember generate resource categories   

我得到以下答案和测试:

  create app/models/category.js
  create app/routes/categories.js
  create app/templates/categories.hbs

ember generate resource category

我得到以下答案和测试:

  create app/routes/category.js
  create app/templates/category.hbs
  identical app/models/category.js

router.js 文件有:

Router.map(function() {
  this.resource('categories', function() { });
  this.resource('category', { path: 'categories/:category_id' }, function() { });
});

当我访问dev:4200/categories/1 时,我收到以下错误:

 GET http://dev:4200/categories/1 404 (Not Found)
 Error while processing route: category.index

谁能告诉我我错过了什么? 所有文件均由 ember-cli 以默认状态生成。

编辑:我有一个models/category.js

export default DS.Model.extend({
  name: DS.attr()
});

routes/category.js

export default Ember.Route.extend({
  model: function(params){
    console.log({category_id: params.category_id});
//    return this.store.find('category', params.category_id);
  }
});

路由dev:4200/categories/1 有效,console.log 显示值Object {category_id: "1"},但return this.store 出现上述错误。

【问题讨论】:

    标签: ember.js ember-cli


    【解决方案1】:

    我通过创建一个 FixtureAdapter 并向模型添加一些夹具来解决它。

    ember generate adapter application

    export default DS.FixtureAdapter.extend({    
    });
    

    models/category.js

    var Category = DS.Model.extend({
      name: DS.attr('string'),
      img: DS.attr('string'),
      cnt: DS.attr('number')
    });
    
    Category.reopenClass({
        FIXTURES: [
            {
                id: 1,
                name: "Writing a blog in Ember",
                cnt: 8,
            },
            {
                id: 2,
                name: "Writing a blog",
                cnt: 5
            }
        ]
    });
    
    export default Category;
    

    该错误具有误导性。现在它工作得很好。谢谢。

    【讨论】:

      【解决方案2】:

      我认为您期待的是 route 而不是 resource 的行为。

      下面会生成两条路由categorycategory.index

      this.resource('category', { path: '/categories/:category_id' }, function() { });
      

      导航到http://dev:4200/categories/1 将首先引导您通过CategoryRoute 然后CategoryIndexRoute

      试试这个:

      Router.map(function() {
        this.resource('categories', function() { });
        this.route('category', { path: '/categories/:category_id' }, function() { });
      });
      

      或者更规范地说。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多