如何在angular2中使用子域进行路由? [英] how to route using subdomain in angular2?

查看:65
本文介绍了如何在angular2中使用子域进行路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有不同用户登录页面的Angular 2应用,例如"user1.example-domain.com"和"user2.example-domain.com"将具有不同且不相关的主页".

我的问题:

  1. 如何使用Angular 2做到这一点?
  2. 如何在本地计算机上对其进行测试? "user1.localhost:port"够用吗?

我已经尝试过使用静态路由,例如:

 {path: 'test.localhost:4200/generic-link1', component: GenericLink1Component}

我已经知道它可以在不倒翁中完成.

解决方案

可以轻松完成此操作,但不能使用Angular(或与此相关的任何其他JavaScript路由器)完成.路由到子域需要服务器提供帮助.您需要将user1user2设置为子域,然后在默认文档中,根据您的规则进行路由.

您的Angular应用将驻留在这两个地址中的任意一个,因此在 http://user1处将有一个. example.com/index.html 和另一个 http://user2.example.com .也可能是同一应用.

对于全方位解决方案,您只需更改方案即可使用 http://example.com/user1.

还有另一个原因说明后一种URL方案可能对您更好.如果您是自托管的,则可以创建自己喜欢的内容的子域.但是,如果您使用的是共享主机,则很有可能会受到只能使用的子域数量的限制-可能只有两个.

I'm looking to create an Angular 2 app with different user landing pages, for example 'user1.example-domain.com' and 'user2.example-domain.com' will have different and unrelated 'home page'.

My questions:

  1. How could I do it using Angular 2?
  2. How can I test it on my local machine? is "user1.localhost:port" enough?

I've tried using static routing like so:

 {path: 'test.localhost:4200/generic-link1', component: GenericLink1Component}

I've seen it done in tumbler I know that it could be done.

解决方案

This can easily be done, but not with Angular (or any other JavaScript router for that matter). Routing to a sub-domain requires the server to help out. You need to set up user1 and user2 as sub-domains and then in your default document, route to either based on whatever you rules are.

You Angular app will then reside at either address, so there will be one at http://user1.example.com/index.html and another at http://user2.example.com. It could be the same app as well.

For an all-Angular solution, you should simply change your scheme to use http://example.com/user1.

There is one more reason why the latter URL scheme may be better for you. If you self-host, you can create sub-domains to your heart's content. If, however, you're using shared hosting, there is a pretty high likelihood that you'll be limited to the number of sub-domains you can use - it may be as few as two.

这篇关于如何在angular2中使用子域进行路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆