当前位置: 首页 > 知识库问答 >
问题:

在Flux体系结构中,如何管理客户端路由/url状态?

张子墨
2023-03-14

对于Flux,我们希望所有操作都通过dispatcher,我想这也包括URL更改。您将如何管理flux应用程序中的URL更改?

共有1个答案

司徒阳曜
2023-03-14

[更新]

在研究了一系列React/Flux应用程序后,我得出的结论是,我更喜欢将路由单独处理,并且与Flux正交。该策略是,URL/路由应该确定哪些组件被挂载,组件根据路由参数和其他必要的应用程序状态从存储区请求数据。

“答案原文”

// Mix-in to the top-level component to capture `click`
// events on all links and turn them into action dispatches;
// also manage HTML5 history via pushState/popState
var RoutingMixin = {
  componentDidMount: function() {
    // Some browsers have some weirdness with firing an extra 'popState'
    // right when the page loads
    var firstPopState = true;

    // Intercept all bubbled click events on the app's element
    this.getDOMNode().addEventListener('click', this._handleRouteClick);

    window.onpopstate = function(e) {
      if (firstPopState) {
        firstPopState = false;
        return;
      }
      var path = document.location.toString().replace(document.location.origin, '');
      this.handleRouteChange(path, true);
    }.bind(this);
  },

  componentWillUnmount: function() {
    this.getDOMNode().removeEventListener('click', this._handleRouteClick);
    window.onpopstate = null;
  },

  _handleRouteClick: function(e) {
    var target = e.target;

    // figure out if we clicked on an `a` tag
    while(target && target.tagName !== 'A') {
      target = target.parentNode;
    }

    if (!target) return;

    // if the user was holding a modifier key, don't intercept
    if (!e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey) {
      e.preventDefault();

      var href = target.attributes.href.value;
      this.handleRouteChange(href, false);
    }
  }
};
var ApplicationView = React.createClass({
  mixins: [RoutingMixin],

  handleRouteChange: function(newUrl, fromHistory) {
    this.dispatcher.dispatch(RouteActions.changeUrl(newUrl, fromHistory));
  },

  // ...
});
RouteStore.prototype.handleChangeUrl = function(href, skipHistory) {
  var isFullUrl = function(url) {
    return url.indexOf('http://') === 0 || url.indexOf('https://') === 0;
  }

  // links with a protocol simply change the location
  if (isFullUrl(href)) {
    document.location = href;
  } else {
    // this._router is a route-recognizer instance
    var results = this._router.recognize(href);
    if (results && results.length) {
      var route = results[0].handler(href, results[0].params);
      this.currentRoute = route;
      if (!skipHistory) history.pushState(href, '', href);
    }

    this.emit("change");
  }
}
 类似资料:
  • 在我们公司,我们正在从一个巨大的单体应用程序过渡到微服务架构。这一决定的主要技术驱动因素是能够独立扩展服务的需求和开发的可扩展性——我们有十个Scrum团队在不同的项目(或“微服务”)中工作。 过渡过程很顺利,我们已经开始受益于这种新的技术和组织结构的优势。现在,另一方面,我们正在努力解决一个主要的痛点:如何管理这些微服务之间依赖关系的“状态”。 让我们举一个例子:其中一个微服务处理用户和注册。该

  • 我需要读取/files目录下的文件,然后在websocket客户端上显示该文件的内容。这就是我在MyRouteBuilder.scala中所做的工作: 但是,当我运行它时,会给出以下stacktrace: RouteId ProcessorId处理器运行时间(ms)[route1][route1][File://Files?noop=true][2][route1][convertBodyTo1]

  • 创建Eureka发现客户端和服务器架构。在客户端有userInfo服务CRUD opeartion。它在启动时显示错误。错误是创建在类路径资源[org/springFramework/cloud/autoconfiure/ConfigurationProperty tiesRebinderAutoConfiguration.class]中定义的名为“配置属性Beans”的bean的错误:合并bea

  • 内存管理基础 程序可执行文件的结构 一个程序的可执行文件在内存中的结果,从大的角度可以分为两个部分:只读部分和可读写部分。只读部分包括程序代码(.text)和程序中的常量(.rodata)。可读写部分(也就是变量)大致可以分成下面几个部分: .data: 初始化了的全局变量和静态变量 .bss: 即 Block Started by Symbol, 未初始化的全局变量和静态变量(这个我感觉上课真的

  • The AdminClient API supports managing and inspecting topics, brokers, acls, and other Kafka objects. To use the AdminClient API, add the following Maven dependency: <dependency>     <groupId>org.apac

  • 我正在阅读关于Flux的文章,但是Todo应用程序的例子太简单了,我无法理解其中的一些关键点。 想象一下,像Facebook这样的单页应用程序拥有用户配置文件页面。在每个用户配置文件页,我们想显示一些用户信息和他们的最后一个帖子,无限滚动。我们可以从一个用户配置文件导航到另一个用户配置文件。 在Flux架构中,这将如何对应于存储和调度器? 我们是为每个用户使用一个,还是使用某种全局存储?那么dis