0

我在使用 ember.js 指定路由器的位置属性时遇到了一些问题。我有以下代码,如果我没有将 App.Router 的位置设置为“历史记录”,则该代码有效。当我设置它时,当它尝试加载页面时,控制台显示以下错误:未捕获的错误:没有路由匹配 URL '/test/' - 其中 /test/ 是我在 /var/www/ 中的目录。这是代码:

应用程序.js

window.App = Em.Application.create({
title: 'Title'
});


App.ApplicationView = Ember.View.extend({
  templateName: 'application'
 })

 App.ApplicationController = Ember.Controller.extend({
   name: 'test'
 })

 App.IndexAboutView = Ember.View.extend({
   templateName: 'index/about'
 })

 App.IndexAboutController = Ember.Controller.extend({
    str: 'my string'
 })

 App.IndexBioView = Ember.View.extend({
   templateName: 'index/bio'
  })

  App.IndexBioController = Ember.Controller.extend({
    str: 'bio text'
 })

 App.Router.reopen({
  location: 'history'
 });

 App.Router.map(function(match){
  this.resource('index', { path: '/' }, function() {
    this.route('about');
    this.route('bio');
   })
  })

我的索引文件看起来像:

   <script type="text/x-handlebars" data-template-name="application">


        <div class="navbar navbar-fixed-top">

            <div class="navbar-inner">

                <div class="container">
                    <a class="btn btn-navbar collapsed" data-toggle="collapse" data-target=".navbar-responsive-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </a>
                    <div class="brand">
                          {{#linkTo "index"}}{{unbound App.title}}{{/linkTo}}   
                    </div>
                    <div class="nav-collapse navbar-responsive-collapse collapse">
                        <ul class="nav">
                            <li><a href="#">a</a></li>
                            <li><a href="#">b</a></li>
                            <li><a href="#">c</a></li>
                        </ul>

                    </div>
                </div>
            </div>
        </div>

        <div class="container">
          {{outlet}}
        </div>
    </script>

    <script type="text/x-handlebars" data-template-name="index/about">
      This is the str from about : {{str}}
    </script>

    <script type="text/x-handlebars" data-template-name="index/bio">
      This is the str from bio : {{str}}
    </script>       
4

3 回答 3

1

尝试在路由器上设置 rootURL 属性。就像是:

App.Router = Ember.Router.extend({
  rootURL: '/test',
  location: "history"
});

这告诉路由器从浏览器历史记录中的相对路径开始。

于 2013-01-25T20:41:29.367 回答
0

这对我有用:

App.Router.reopen({
  location: 'history'
})

其中 App 是我的应用程序常量名称,它出现在App.Router.map...

于 2013-08-21T20:13:21.080 回答
0

将 /test/ 添加到您的索引路径,我在这里添加了一个工作示例而不是 /test/ 我必须匹配 jsbin 的默认根 url,即/iyexuf/3/edit

App.Router.map(function(match){
  this.resource('index', { path: '/test/' }, function() {
    this.route('about');
    this.route('bio');
   })
  })

我添加了一个指向 about 和 bio 的链接来说明三个部分(标题、关于、bio)之间的导航

{{#linkTo "index.about"}}About{{/linkTo}}   
{{#linkTo "index.bio"}}Bio{{/linkTo}}  
于 2013-01-25T19:36:25.220 回答