Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干应用程序未初始化_Javascript_Backbone.js_Model View Controller_Routes - Fatal编程技术网

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;