简体   繁体   English

在没有“#”且没有副作用的情况下进行路由时,Angular 4如何更改URL?

[英]How can Angular 4 change URL when routing without “#” and no side effects?

In previous versions of angular, or other SPA frameworks, navigations used to use "#" in the URL to move from a page to another without make another request to the server. 在angular或其他SPA框架的早期版本中,导航曾经使用URL中的"#"从一个页面移动到另一个页面,而无需再次向服务器发出请求。

Today, using Angular 4 routes, I notice that the URL was rewritten without any request to the server. 今天,使用Angular 4路由,我注意到URL被重写,而没有任何对服务器的请求。 for instance domain.com/about . 例如domain.com/about It seems very strange to me. 对我来说似乎很奇怪。

If my SPA is served from a domain.com/index.html , a request to domain.com/about should have a side effect bring a totally diferente page (for instance about/index.hmtl ), not a route inside the index.html . 如果我的SPA是从domain.com/index.html ,则对domain.com/about的请求应具有副作用,这会导致页面完全不同(例如about/index.hmtl ),而不是index.html内部的路由index.html

Change URL without redirect to the address in the URL seems to violate something.. dont? 更改URL而不重定向到URL中的地址似乎违反了某些规定。

How Is that possible in Angular 4 using only client side? 仅使用客户端,在Angular 4中怎么可能?

While you navigate on site, click links, tabs etc. - this all is just client side. 当您在网站上浏览时,单击链接,选项卡等-所有这些都只是客户端。 However, when you press F5 or navigate to link like /school/6/personList - server should be configured to return index.html. 但是,当您按F5键或导航至/ school / 6 / personList之类的链接时-服务器应配置为返回index.html。

Thats why quite often rest api is smth like /api/users, /api/emails, etc. so all start with '/api' - and you may easily redirect all other requests to index.html. 这就是为什么休息api经常像/ api / users,/ api / emails等一样,所以都以'/ api'开头的原因-您可以轻松地将所有其他请求重定向到index.html。

Change URL without redirect to the address in the URL seems to violate something.. dont? 更改URL而不重定向到URL中的地址似乎违反了某些规定。

No, that's the point of Single Page Applications (SPA) as opposed to the other way where a request is sent to the server for each URL change. 不,这是单页应用程序(SPA)的要点,与之相反,对于每个URL更改,将请求发送到服务器。

Angular router has two location strategies: 角路由器有两种定位策略:

navigations used to use "#" in the URL to move from a page to another without make another request to the server. 导航曾经使用URL中的“#”从一个页面移动到另一个页面,而不向服务器发出另一个请求。

It's still possible now with Angular by using HashLocationStrategy , to do that just use {useHash: true} do the following: Angular现在仍然可以通过使用HashLocationStrategy来做到这一点,只需使用{useHash: true}执行以下操作:

@NgModule({
    imports: [
        RouterModule.forRoot(appRoutes, {useHash: true})
    ],

Today, using Angular 4 routes, I notice that the URL was rewritten without any request to the server 今天,使用Angular 4路由,我注意到URL被重写,而没有对服务器的任何请求

That's because the PathLocationStrategy is used by default. 这是因为默认情况下使用PathLocationStrategy

Regardless of the strategy, if you're using Angular routing, all URL changes from the application will be handled by the router and no requests will be sent to the server. 不管采用哪种策略,如果您使用的是Angular路由,则来自应用程序的所有URL更改都将由路由器处理,并且不会将任何请求发送到服务器。 For example, the following code: 例如,以下代码:

Router.navigateByUrl('/my')

in the case of PathLocationStrategy will generate the following URL: 对于PathLocationStrategy将生成以下URL:

host/my

and in the case of HashLocationStrategy it will generated ULR like this: HashLocationStrategy的情况下,它将生成如下所示的HashLocationStrategy

host#my

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在布尔值的子类化中更改字符串表示形式,而又不会在函数外部产生负面影响? - How can I subclass Boolean to change string representation without negative side effects outside of my function? Angular + Ionic:没有侧边菜单,URL路由不起作用 - Angular + Ionic : URL routing is not working without side menu 网址中的角度路由更改 - Angular routing change in url 如何在JavaScript中处理没有副作用的JSON数据? - How can I manipulate JSON data without side effects in JavaScript? 如何在没有副作用的情况下进行多个查询 - How can i make several queries without side effects 在URL中没有哈希字符的情况下,angular2的spa路由如何工作? - How is the spa routing of angular2 working without the hash character in the url? 无副作用更改 mongoose 对象的最佳方法 - Best way to change mongoose objects without side-effects 如何在 function 中更改路由 url? - How can I change the routing url within a function? 如何更改Angular中的url参数? - How can I change url parameters in Angular? 不在路由URL中时禁用并绕过角度路由 - Disable and bypass the angular routing when not in route url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM