【问题标题】:How to configure Office-js excel react add-in to use react-router-dom or alternative solutions?如何配置 Office-js excel react 插件以使用 react-router-dom 或替代解决方案?
【发布时间】:2019-09-24 01:40:57
【问题描述】:

因此,我正在尝试使用 react 设置 Office-js excel 加载项(使用在此处找到的 yeoman office-js 生成器 https://github.com/OfficeDev/generator-office),并且在配置加载项以使用多个路由时遇到了问题。看起来不像传统的 React 路由开箱即用(目前正在尝试使用 react-router-dom)。有人知道我会怎么做吗?特别是,看看我应该如何配置某种路由、webpack.config.js 和 manifest.xml。

例如,希望在 route=[baseUrl]/ 上加载 LandingComponent 之类的东西,在 [baseUrl]/signup 上加载类似 SignupComponent 之类的东西。

对于常规的旧 React,我正在尝试做的事情看起来像这样

const Routes = () => (
  <div>
    <Route path="/" exact component={LandingComponent} />
    <Route path="/signup" exact component={SignupComponent} />
  </div>
)

我怀疑需要修改的关键代码段可能涉及 webpack.config.js 中的某些内容(对于实际配置 webpack 来说非常新,不确定我是否需要与这个人打交道),

manifest.xml

<DefaultSettings>
    <SourceLocation DefaultValue="https://localhost:3000/taskpane.html"/>
</DefaultSettings>

此外,我正在考虑从上面的 URL 中删除“.html”之类的事情(目标是插件应该默认在“https://localhost:3000/”加载登陆,并且您可以通过按钮导航到“ https://localhost:3000/signup',而插件当前默认加载'https://localhost:3000/taskpane.html')。

对不起,呕吐这个词,在这方面还是很新的,不知道什么是可能的,什么是不可能的!

【问题讨论】:

  • 路由在 Office 加载项中的工作方式应该与在任何 React Web 应用程序中的工作方式相同。请提供一些有关问题所在的详细信息。
  • 嗨,Rick,信不信由你,听完这个事实真的让我松了一口气,我认为这可能取决于我更好地掌握 webpack 并弄清楚这一点。只会修补一点,可能会说这不再是 office-js 问题。谢谢!欣赏!
  • @sch.根据我的经验,你不需要对 webpack 做任何事情。您必须在路由上添加 HashLocationStrategy 技术。因为它是在办公室 iframe 路由器内部加载,所以它不起作用。但是如果你添加 HashLocationStrategy 那么它会起作用。
  • reacttraining.com/react-router/web/api/HashRouter。请看一看。希望它可以节省您探索 webpack 的时间
  • @RagavanRajan 非常感谢您的提示!它肯定派上用场了!

标签: reactjs office-js react-router-dom


【解决方案1】:

我遇到了同样的问题,我使用的解决方案基本上是遵循@Ragavan Rajan 使用HashRouter 而不是BrowserRouter 的解决方案。

import {
  HashRouter as Router,
  Switch,
  Route
} from "react-router-dom";


...
render() {
return (
<Router>
  <div>
    <Switch>
      <Route exact path="/main" component={Home} />
    </Switch>
  </div>
</Router>
);
}

This answer 提供更多见解。最终,用于维护您导航过的历史记录的两个函数设置为 null:

window.history.replaceState = null;
window.history.pushState = null;

当您尝试使用正常的浏览器路由时,如果由于这些功能不存在而引发异常,哈希路由不会发生这种情况。

【讨论】:

  • 最新的 HashRouter(在 react-router-dom 中)似乎通过 react-router (正在调用历史)间接使用 replaceState。 Uncaught TypeError: p.replaceState is not a function at createHashHistory (index.js?3c0a:11) at HashRouter (index.js?066a:5) at HashRouter (react-hot-loader.development.js?9cb3:830) 可能是因为我使用的是react-hot-loader,但是使用HashRouter对我不起作用。
  • @kotpal 我也发生了同样的事情,使用 v5 版本的 react-router 解决了什么问题
猜你喜欢
  • 2021-04-03
  • 2018-07-13
  • 2022-11-04
  • 2021-02-04
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
  • 2022-09-22
  • 2017-08-23
相关资源
最近更新 更多