我正在尝试在我的React单页面应用程序中实现Google Analytics,但要做到这一点,我知道我需要使用history属性。我正在使用交换机设置,但这似乎不起作用。
<Switch history={history}>
<Route exact path="/" component={place}/>
<Route path="/about" component={otherplace}/>
<Route component={error}/>
</Switch>如果我将<Switch>更改为<Router>,则分析可以正常工作,但错误页面会在每个页面上呈现,并且当URL路径更改时,页面需要刷新。
发布于 2018-10-31 20:00:42
问题是错误组件在所有路由上。
试试这个:添加一个不同的404错误路由:
<Route path='/404' component={error} />将所有不匹配的路由路由到它:
<Redirect from='*' to='/404' />这应该只在不存在的页面上显示错误组件。
这里有一个开关可以做到这一点:
<Switch history={history}>
<Route exact path="/" component={place}/>
<Route path="/about" component={otherplace}/>
<Route path='/404' component={error} />
<Redirect from='*' to='/404' />
</Switch>https://stackoverflow.com/questions/53081771
复制相似问题