2017-02-21 22 views
7

当我在应用中构建和导航链接时,一切正常,但在地址栏中输入却不行! 最终,我希望能够通过电子邮件发送指向特定应用程序页面/路径的链接,但我不确定如何完成此操作。我已按照angular router guide documentation。正确的,我认为...Angular 2.0深度链接不起作用。地址栏中相同

我使用NodeJS(与快递),并在服务器中我已重定向到应用程序根的所有流量。

app.get("*", function(req, res) { 
    console.error("in get *") 
    res.redirect("/"); 
}); 

以我的index.html我已经设置基地

<base href="/"> 

我有我的客户击溃/路径如下

const appRoutes: Routes = [ 
    { path: 'vendor/registration', component: VendorRegistrationComponent }, 
    { path: 'vendors', component: VendorListComponent }, 
    { path: '', redirectTo: 'vendor/registration', pathMatch: 'full' }, 
    { path: '**', component: PageNotFoundComponent }  
]; 

如果我在浏览器的地址栏I型http://localhost:8080/vendors设置得到http://localhost:8080/vendor/registration哪种是有道理的,因为这是服务器告诉浏览器重定向到的地方。

那么我应该如何深入链接到我的应用程序?

编辑。 Angular教程 - “英雄之旅” - 也表现出相同的行为。即直接在浏览器的地址栏中输入网址不起作用。该应用程序显示一个“加载...”文本,并不会路由到控制器。

+0

明确地描述你的深层链接意思?你期望得到什么? – echonax

+0

我想要的是能够向用户发送电子邮件链接,例如http://www.example.com/vendors,并显示正确的视图/控制器(而不是根元素) – nsof

+0

你的代码是什么? app.get('/',function(req,res){/ * what's here * /})' – idbehold

回答

1

你的问题的回答在angular2 documentation

+0

谢谢。由于某些原因,这未在[link](https://angular.io/docs/ts/latest/guide/router.html)中引用或提及。 此外,我实际上尝试过,但引用了错误的'index.html',而不是'/ dist'文件夹中的一个,我引用'/ src'文件夹中的一个。 – nsof

2

通常,您不需要在服务器上重定向,因为您的所有路由都由客户端上的角路由器处理。

取而代之的是让您的所有快速路线始终为您所有请求的网址提供index.html。当客户端应用程序引导时,angular会根据请求的url来照顾到适当的组件。请务必保留您的<base href="/">标签,以便角度可以知道url的哪部分路由。

const path = require('path'); 

app.get("*", function(req, res) { 
    res.sendFile(path.join(__dirname, '/path/to/your/index.html')); 
}); 
+0

这终于对我有以下**重要提示**:发送到客户端的'index.html'文件应该位于'/ dist'文件夹中,而不是原来'/ src'文件夹。 (至少对于具有默认设置的节点+快捷项目) 'var indexFile = path.join(__ dirname,“dist/index.html”); res.sendFile(indexFile);' – nsof