我试图在ember app kit中定义嵌套资源。我的文件结构不正确吗?当我添加嵌套资源时,我得到以下异常:
Error: Assertion Failed: The URL '/pages.index' did not match any routes in your application
通过简单地注释掉定义嵌套“页面”资源的函数,应用程序正确加载并显示页面模板。
路由器代码:
var Router = Ember.Router.extend();
Router.map(function() {
this.route('component-test');
this.route('helper-test');
this.resource('pages', {path: '/pages'}
// if this line is commented out, no error (index route is not called though)
, function() { this.resource('page', {path: ':page_id'}); }
);
});
export default Router;
文件结构因此:
$ ls -R
component-test.js helper-test.js pages
component_test.js index.js pages.js
./pages:
index.js page.js
页面路线:
export default Ember.Route.extend({
model: function() {
return [{title: "One", _id: "one"}, {title: "Two", _id: "two"}];
//this.store.find('page');
}
});
pages / index route:
export default Ember.Route.extend({
model: function() {
return this.modelFor('page');
}
});
页面/索引路径的es6生成模块如下所示:
define("appkit/routes/pages/index",
["exports"],
function(__exports__) {
"use strict";
__exports__["default"] = Ember.Route.extend({
model: function() {
return this.modelFor('page');
}
});
});
答案 0 :(得分:4)
试试这个。
您是否在页面文件夹中有名为“index.js”的页面索引路径?
Ember App Kit按文件夹结构查找文件。所以页面索引路由应该在“app / routes / pages / index.js”中找到。
以下是此代码https://github.com/kiwiupover/for_eric
的回购路由器
var Router = Ember.Router.extend();
Router.map(function() {
this.route('component-test');
this.route('helper-test');
this.resource('pages', function() {
this.route('new');
});
});
export default Router;
路线文件夹
routes -| //folder
pages.js
pages -| // folder
index.js
页面路由
export default Ember.Route.extend({
model: function(){
return [{title: "One", _id: "one"}, {title: "Two", _id: "two"}];
}
});
页面索引路径
export default Ember.Route.extend({
model: function(){
return this.modelFor('pages');
}
});
模板文件夹
templates -| //folder
pages.hbs
pages -| // folder
index.hbs
页面模板
<h1>Pages</h1>
<ul>
{{#each}}
<li>{{title}}</li>
{{/each}}
</ul>
{{outlet}}
索引模板
<h2>The Index page for Pages</h2>
<ul>
{{#each}}
<li>the index page {{title}}</li>
{{/each}}
</ul>