【问题标题】:Simple ember routing - how to define multiple routes?简单的 ember 路由 - 如何定义多个路由?
【发布时间】:2013-06-14 20:06:50
【问题描述】:

好的,伙计们-真的不应该比这更简单。我定义了一个名为 about 的路由,并在我的模板中添加了一个 linkTo about,通过插座运行它,ember 按预期工作。

然后我添加了另一个名为 foobars 的路由,对它做了同样的事情并得到了一个未捕获的错误:

Uncaught Error: assertion failed: The attempt to linkTo route 'foobars' failed. The router did not find 'foobars' in its possible routes: 'about', 'index' 

这是我的余烬

App = Ember.Application.create()

App.Router.map(function(){
    this.resource('about');
    this.resource('foobars');
});

我的死简单 html

<body>
<h1>ember</h1>

<script type="text/x-handlebars">
  <h2>application template</h2>
  <a>{{#linkTo 'about'}} about {{/linkTo}}</a>
  <a>{{#linkTo 'foobars'}} foobars {{/linkTo}}</a>
  {{ outlet }}
</script>

<script type="text/x-handlebars" id="about">
  <h2>about template</h2>
</script>

<script type="text/x-handlebars" id="foobars">
  <h2>foobars template</h2>
</script>

就像我说的,它适用于 about 模板,所以我知道我的配置没问题。我也尝试过单独添加它们,如下所示:

App.Router.map(function(){
    this.resource('about');
});

App.Router.map(function(){
    this.resource('foobars');
});

我希望定义两条路线与定义一条路线没有太大不同,但我似乎没有理解一些东西。有人可以指出我的理解错误吗?谢谢!

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    我认为您只是在重新加载页面之前没有保存文件。我尝试了您的示例,它对我来说效果很好。

    但是,当我注释掉 foobars 路由时:

    App = Ember.Application.create();
    
    App.Router.map(function() {
      this.resource("about");
      //this.resource("foobars");
    });
    

    我在控制台中遇到了同样的错误:

    Error: assertion failed: The attempt to linkTo route 'foobars' failed. The router did not find 'foobars' in its possible routes: 'about', 'index'
    

    【讨论】:

    • 好的,是的,它是类似的——我实际上有 2 个文件在运行,而我正在编辑的那个在根目录中......不知何故。谢谢。
    【解决方案2】:

    你需要在表单中定义路由:

    App.FoobarsRoute = Ember.Route.extend({
        model: function() {
            return App.Foobars.find();
        }
    });
    

    这通常会放在自己的文件中:/routes/foobars_route.js

    【讨论】:

    • 特定错误与路由处理程序无关。只是抱怨找不到名为foobars的路由。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2015-10-15
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    相关资源
    最近更新 更多