2017-02-21 54 views
1

我想在Ionic 2中实现导航。我尝试过使用DeepLinking,并且得到了结果,但'#'符号正在URL中。 当'#'符号出现在URL中时,Google Analytic将无法识别网站,这就是为什么我试图以不同的方式实现导航,例如Angular 2 Routing,它同时支持(HTML5或哈希URL样式),但无法实现in Ionic 2.如何通过'#'克服在Ionic 2中通过DeepLinking进入URL?

Ex-http://localhost:8100/#/registration - 这个工作正常,但我想没有'#'。 像http://localhost:8100/registration

感谢您的帮助

回答

1

尝试使用pathLocationStrategy而不是HashLocationStrategy。

在app.module.ts

import { LocationStrategy, 
     PathLocationStrategy } from '@angular/common'; 

... 

@NgModule({ 
... 
providers: [ 
{ 
    provide: LocationStrategy, 
    useClass: PathLocationStrategy 
}, 
... 

或者其他的方式添加,这是

IonicModule.forRoot(MyApp, { 
     locationStrategy: 'path' 
    }) 

,并确保有一个有效的基本href。

+0

我试图在这两个,但没有工作,给运行时错误 - \t @NgModule({ \t \t进口:[IonicModule.forRoot( MyApp的,{locationStrategy: '路径'},{ \t \t链接:[ \t \t \t {组分:RegistrationComponent,名称: '注册',段: '登记'} \t \t] \t \t})], \t \t提供商:[...] \t}) \t @NgModule({ \t \t进口:[IonicModule。forRoot(MyApp的,{},{ \t \t链接:[{组分:RegistrationComponent,名称: '注册',段: '登记'},] \t \t})], \t \t提供商:[{提供:LocationStrategy ,useClass:PathLocationStrategy},...] \t}) –

+0

嗨,我需要传递一个参数,例如'http:// localhost:8100/feedback/767',我该怎么做? –

+0

此方法对我无效 –

0

所以这里列出了我所做的事情。希望这可以帮助。

  1. 我们需要删除每个网址的路径中的#号,因为Google Analytics拒绝其中包含#个网址。在APP模块,{locationStrategy: '路径'}添加到你的应用程序模块,如下所示:

    IonicModule.forRoot(MyApp, { 
        locationStrategy: 'path' 
    }) 
    

2。现在#从URL中删除。但是,当你刷新或直接访问网址,这将无法正常工作,因为这是任何SPA的预期行为。刷新页面时,服务器试图在所提及的位置查找页面。正如@Parth Ghiya上面所说的对于例如:如果你打localhost/abc,那么服务器会试图找到abc/index.html实际上不存在的。所以为了解决这个问题,你在我的服务器上写了配置,请求index.html。我正在使用节点快递服务器来部署应用程序。使用下面的代码来路由每个请求的index.html -

var express = require('express'); 
var path = require('path') 
var app = express(); 
app.use(express.static(path.resolve(__dirname, "www"))); 

app.use('/*', function(req, res){ 
    res.sendFile(__dirname+ '/www' + '/index.html'); 
}); 

app.set('port', process.env.PORT || 3000); 
app.listen(app.get('port'), function() { 
    console.log("listening to Port", app.get("port")); 
});