[英]Single page app url management
Usually in a Single Page App(spa), I do have a single page have where I have a sidenav menu. 通常在单页应用程序(spa)中,我有一个页面,我有一个sidenav菜单。 In that menu, there are multiple anchor tags.
在该菜单中,有多个锚标签。
Those anchor tag's url will be handled by angular/react/sammy js router, and the main div would be refreshed based on the returned html from the controller. 这些锚标记的url将由angular / react / sammy js路由器处理,主div将根据控制器返回的html进行刷新。
Pretty simple, right? 很简单吧?
But imagine a scenario, where user directly access the anchor tag url via browser address bar. 但想象一下场景,用户通过浏览器地址栏直接访问锚标记URL。 then only the returned html segment would be loaded to the whole page.
然后只将返回的html段加载到整个页面。
Is there any way to handle this kind of situation; 有没有办法处理这种情况; I mean so that whenever user directly access the url, he/she would be properly addressed?
我的意思是,无论何时用户直接访问网址,他/她都会得到妥善解决?
EDIT: May be I'm not so clear about the problem statement. 编辑:可能我不太清楚问题陈述。 Let me elaborate a bit:
让我详细说明一下:
My question is, can we implement some design approach, so that these two works well? 我的问题是,我们可以实施一些设计方法,以便这两种方法运作良好吗?
With React router you can simply handle onEnter
event, fired when new URL is given by user. 使用React路由器,您可以简单地处理
onEnter
事件,在用户给出新URL时触发。 You can do ie: 你可以这样做:
const onEnter = (e) => {
console.log(e.location.pathname);
}
/* ... */
<Route path="/" onEnter={onEnter} component={MyComponent} />
This sample should log new URL in console while it is given. 此示例应在给定时在控制台中记录新URL。 You can interpret it further as you want for making proper mutation.
您可以根据需要进一步解释它以进行适当的变异。
Better go for angular ui-router
instead of sammy js router
, Of course I don't know about it. 最好去角度
ui-router
而不是sammy js router
,当然我不知道它。 But in angular-ui-router
, we define states and urls by using $stateProvider
of ui-router
. 但是在
angular-ui-router
,我们通过使用ui-router
$stateProvider
来定义状态和URL。 Even user entered the url directly in the browser, he wil be addressed correctly only if the url defined in the state matched. 即使用户直接在浏览器中输入了网址,只有在状态中定义的网址匹配时才会正确处理。
https://github.com/angular-ui/ui-router/tree/legacy see here for more info. https://github.com/angular-ui/ui-router/tree/legacy请点击此处获取更多信息。
Here is an example, 这是一个例子,
$stateProvider.state('',{
url: '/',
tempate: 'path/to/.html',
controller: 'controllerToHandle'
})
.state('home', {
url: '/home',
template: 'path/to/home.html',
controller: 'HmeCtrl'
})
.state('home.list',{ //defines child states with dot
url: '/list', //shows url as home/list because it is a child state
template: 'path/to/list.html'
})
Use $urlRouterProvider.otherwise('/')
if no path matched Likewise, user can traverse from home
to list
for example, www.my-app.com
is your host name. 如果没有匹配的路径,请使用
$urlRouterProvider.otherwise('/')
同样,用户可以从home
遍历到list
,例如, www.my-app.com
是您的主机名。 If he entered www.my-app.com/home/list
as url /home/list
matched, he will be taken to home/list
page and if he entered any wrong url, then he will be taken to home
page because of otherwise
method. 如果他输入
www.my-app.com/home/list
作为url /home/list
匹配,他将被带到home/list
页面,如果他输入了任何错误的url,那么他将被带到home
, otherwise
方法。
If there is something like this in the react/sammy router, do that for better solution 如果react / sammy路由器中存在类似的内容,请执行此操作以获得更好的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.