2017-02-21 53 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")); 
});