微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

BackboneJS 路由器不呈现视图

如何解决BackboneJS 路由器不呈现视图

我将 BackboneJS 与 TypeScript/Vite 一起使用,并有以下文件一个路由器和一个视图:

import Backbone from "backbone";
import { AboutView } from "./views/about";
import { MainView } from "./views/main";

export class Router extends Backbone.Router {
  constructor(options?: Backbone.RouterOptions) {
    super(options);

    this.routes = {
      "": "home","about": "about"
    };
  }
  
  home() {
    app.Views.Home = new MainView({
      el: "#app"
    });
    app.Views.Home.render();
  }

  about() {
    app.Views.About = new AboutView({
      el: "#app"
    });
    app.Views.About.render();
  }
};

这是我的MainView

import Backbone from "backbone";
import template from "lodash/template";

export class MainView extends Backbone.View {
  template = `
  <h1>Hello,<%= name %>!</h1>
  <button id="aboutlink">About</button>
  `;

  events() {
    return {
      "click #aboutlink": "aboutClick"
    }
  }

  options: Backbone.ViewOptions;
  constructor(options: Backbone.ViewOptions) {
    super(options);
    this.options = options;
  }

  render() {
    const tmpl = template(this.template);
    this.$el.html(tmpl({ name: "Name" }));
    return this;
  };

  aboutClick() {
    app.Router?.navigate("about");
  }
};

最后是 bootstrap.ts,它包含在 HTML 文件正文的末尾:

import Backbone from "backbone";
import { Router } from "./router";
import "./style.css";

app.Router = new Router();
Backbone.history.start();

然而,当我在浏览器中导航到 Vite 开发服务器时,我只看到一个空白页面。控制台中没有错误。这里发生了什么?

谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。