Javascript 主干应用程序未初始化
我正在开发一个主干应用程序。我有以下应用程序文件:Javascript 主干应用程序未初始化,javascript,backbone.js,model-view-controller,routes,Javascript,Backbone.js,Model View Controller,Routes,我正在开发一个主干应用程序。我有以下应用程序文件: // src/app.js var AppRouter = require('./router'); var App = new AppRouter(); 它链接到索引页(链接有效,我已验证): 当我导航到页面的索引时,routeIndex事件不会发生(控制台中没有任何记录)。另外,当我导航到/hello时,警报也不会弹出。我做错了什么? 在页面加载期间,在应用程序完成其所有路由器的创建之后,请确保调用Backbone.history.st
// src/app.js
var AppRouter = require('./router');
var App = new AppRouter();
它链接到索引页(链接有效,我已验证):
当我导航到页面的索引时,routeIndex事件不会发生(控制台中没有任何记录)。另外,当我导航到/hello
时,警报也不会弹出。我做错了什么?
在页面加载期间,在应用程序完成其所有路由器的创建之后,请确保调用Backbone.history.start()或Backbone.history.start({pushState:true})来路由初始URL
这似乎解决了路由索引和索引路由的问题。然而,当我在浏览器中导航到
/hello
时,我会看到一个页面,上面写着“cannotget/hello”,我明白了原因。我需要使用散列片段URL/#/你好工作。
<!DOCTYPE html>
<html>
<head>
<title>Feedback Dev Test</title>
</head>
<body>
<script type="text/javascript" src="assets/js/app.js"></script>
</body>
</html>
var Backbone = require('backbone');
var $ = require('jquery/dist/jquery');
var IndexView = require('./views/index');
Backbone.$ = $;
var AppRouter = Backbone.Router.extend({
routes: {
'': 'routeIndex',
'hello': 'hello'
},
routeIndex: function() {
if (!this.indexView) {
console.log('routeIndex called');
this.indexView = new IndexView();
}
},
hello: function() {
alert('HELLO');
}
});
module.exports = AppRouter;