【问题标题】:Routing a modular app with Ampersand.js?使用 Ampersand.js 路由模块化应用程序?
【发布时间】:2015-04-06 09:47:18
【问题描述】:

我正在尝试使用 Ampersand.js 编写我的第一个模块化应用程序,但我遇到了一些问题!希望有人能指出我正确的方向......

好的,所以我已将WatchifyBrowserify 配置到我的html 文件中,以创建一个bundled.js 文件,每次我进行更改时都会重新编译该文件。然后我想添加一个&符号模块-ampersand-router(在终端中安装为npm install --save ampersand-router,以指定我想要处理的所有应用程序路由,并在用户遇到不可用路由时重定向用户。

问题在于没有重定向我的 # 个路由,我不知道为什么!因为我没有服务器(或者这个项目需要一个服务器)我正在尝试使用基于 # 的路由,我希望它处理三个路由,一个空路由(我的起点),gif/id 路由一个catch 将重定向到起点的路线。

所以我的文件结构包含一个 router.js:-

'use strict';
var AmpersandRouter = require('ampersand-router');
module.exports = AmpersandRouter.extend({
routes:{
    '': 'gifs', //empty
    'gif/:id': 'gif',
    'catch': 'catch' //catch and redirect
},
gifs: function(){
    console.log('gifs page');
},

gif: function(id){
    console.log('gif page', id);
},
'catch': function(){
    this.redirectTo('');
}

});

还有一个 app.js 文件:-

var Router = require('./router.js');
window.app = {
init: function () {
    console.log('hello world');
    this.router = new Router();
    this.router.history.start();
}
};
window.app.init();

这都是在我下面的 index.html 和 package.json 代码中组装的 <script src = "bundled.js"></script>

"scripts": {
"start": "watchify js/app.js -o bundled.js",
"build": "browserify js/app.js -o bundled.js"

任何帮助将不胜感激!谢谢大家!

【问题讨论】:

    标签: javascript node.js modular ampersand.js


    【解决方案1】:

    会发生什么?有什么错误吗?另外,您可以尝试进入项目聊天并在那里提问:https://gitter.im/AmpersandJS/AmpersandJS

    【讨论】:

      【解决方案2】:

      尝试禁用pushState

      this.router.history.start({pushState: false});
      

      启用后,#-fallback 不会启动。

      【讨论】:

        【解决方案3】:

        您是否尝试在基本目录中使用“npm start”?

        我没有将 Ampersand.js 放入 HTML 文件,但它似乎可以使用纯 Javascript。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-23
          • 1970-01-01
          • 2016-10-03
          • 2017-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多