EmberJS 指定URL类型

2018-01-03 14:30 更新

指定URL类型

路由器还使用浏览器的历史API来定位之前使用的页面。您也可以停用位置API。

Ember.Router.extend({
  location: 'history'
});

在上面的代码中,路由器使用浏览器的历史 API。

例子

<!DOCTYPE html>
   <html>
      <head>
         <title>Emberjs Specifying the URL Type</title>
            <!-- CDN's-->
            <script src="/attachements/w3c/handlebars.min.js"></script>
            <script src="/attachements/w3c/jquery-2.1.3.min.js"></script>
            <script src="/attachements/w3c/ember-template-compiler.js"></script>
            <script src="/attachements/w3c/ember.min.js"></script>
            <script src="/attachements/w3c/ember.prod.js"></script>
            <script src="/attachements/w3c/ember.debug.js"></script>
      </head>
      <body>
        <script type="text/x-handlebars" data-template-name="application">
             <!-- link-to for navigation between the routes -->
             {{#link-to 'authors'}}AuthorInfo{{/link-to}}
             {{#link-to 'books'}}BookInfo{{/link-to}}
             {{outlet}}
          </script>

           <script type="text/x-handlebars" data-template-name="authors">
              <h2>Authors Page </h2>
              <ul>
                 <li>Herbert Schildt</li>
                 <li>Robert Lafore</li>
              </ul>
           </script>

           <script type="text/x-handlebars" data-template-name="books">
              <h2>Books Page</h2>
              <ul>
                <li>Java</li>
                <li>C++</li>
              </ul>
           </script>

           <script type="text/javascript">
             App = Ember.Application.create();

             App.Router.map(function() {
                //refers to the authors template and path refers within page
                this.route('authors', { path: '/' });
                //refers to the books template
               this.route('books');
             });

             Ember.Router.extend({
             //getting the history of the pages are searched before
                location: 'history'
             });
          </script>
    </body>
 </html>

输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上述代码保存在 routing_history.html 文件中

  • 在浏览器中打开此HTML文件。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号