【发布时间】:2015-04-06 09:47:18
【问题描述】:
我正在尝试使用 Ampersand.js 编写我的第一个模块化应用程序,但我遇到了一些问题!希望有人能指出我正确的方向......
好的,所以我已将Watchify 和Browserify 配置到我的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