2016-04-23 35 views
1

路由我有這個在我的代碼...角2 - 與路徑作爲參數和BrowserSync

@RouteConfig([ 
{path: "/me", name: "Me", component: MeComponent, useAsDefault: true}, 
{path: "/me/edit", name: "EditProfile", component: EditProfileComponent}, 
{path: "/people/:group", name: "People", component: PeopleComponent} 
]) 

但爲了使像/人/學校,我需要設置在browserSync路線。我的第一個嘗試是一個通配符,像這樣:

browserSync.init({ 
    server: { 
     baseDir: "./", 
     routes: { 
      "/me": "index.html", 
      "/me/edit" : "index.html", 
      "/people/*" : "index.html" 
     } 
    } 

它不工作。有關如何配置browserSync以使用動態生成的url路徑的任何建議?

ty!

回答

0

您可以將您的應用程序配置爲使用HashLocationStrategy

bootstrap(AppComponent, [ 
    ROUTER_PROVIDERS, 
    provide(LocationStrategy, { useClass: HashLocationStrategy }) 
]); 

或配置瀏覽器同步rewriteRules重寫你的角2條路由路徑的應用程序根index.html

+0

TY,克里斯!我將檢查重寫規則:)沒有計劃再使用散列 –

+0

OP正試圖在Browsersync下處理HTML5路由。 Browsersync rewriteRules用於html _content_,_not_ URL重寫。 OP需要改爲使用Browsersync中間件。見下面的答案。 – MattEvansDev

0

您需要執行URL重寫,將請求發送到您的HTML 5路由並返回到您的index.html頁面。

var bsConfig = { 
 
    "open": baseUrl, 
 
    "server": { 
 
     //... 
 
    }, 
 
    middleware: function(req,res,next) { 
 

 
     /* 
 
      Re-write URL for each of our Angular routes, mapping them back to index.html (for single-page app, HTML5 routing) 
 
     */ 
 
     switch (req.url) { 
 
      //for all angular routes, re-rewrite to /index.html 
 
      case baseUrl + '/customers' : 
 
      case baseUrl + '/products' : 
 
      case baseUrl + '/transactions' : 
 
      
 
      req.url = '/index.html'; 
 
      
 
      default : { 
 
       //for all other requests (e.g.: to static content such as .css files) don't make any URL re-writing. 
 
       break; 
 
      } 
 
      
 
     } 
 

 
     return next(); 
 
    } 
 
}; 
 

 

 
browserSync.init(bsConfig);

感謝: https://vinaygopinath.me/blog/tech/url-redirection-with-browsersync/