简体   繁体   English

单页应用网址管理

[英]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: 让我详细说明一下:

  • Suppose my page url is: abc.com/dashboard 假设我的页面网址是:abc.com/dashboard
  • This page got a navigation menu and a div section whose class name is "main-container" 这个页面有一个导航菜单和一个div类,其类名是“main-container”
  • User click a link in the nav menu and the router moved the url to for say abc.com/view/listofXYZ. 用户单击导航菜单中的链接,路由器将URL移动到说abc.com/view/listofXYZ。 So, our "main-container" div would be loaded with the response of the url abc.com/view/listofXYZ 因此,我们的“主容器”div将加载url abc.com/view/listofXYZ的响应
  • Now another user, directly go to the abc.com/view/listofXYZ url and hit eneter. 现在是另一个用户,直接转到abc.com/view/listofXYZ url并点击eneter。 Then, the page would contain only the response html of the url ie all nav menu and div are gone. 然后,页面将只包含url的响应html,即所有导航菜单和div都消失了。


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,那么他将被带到homeotherwise方法。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM