有一種方法可以在新Angular2路由器命名路由(不支持)(其主要思想是從檢查答案 - @邁克爾羅賓遜):
文件app.routes.ts我有:
...
import { Url } from './common/url';
export const routes: RouterConfig = [
{ path: '', component: LoginForm },
{ path: Url.to('login'), component: LoginForm },
{ path: Url.to('home'), component: Home, canActivate: [AllAuthGuard] },
{ path: Url.to('clients'), component: Cliens, canActivate: [AdminAuthGuard] },
{ path: Url.to('projects'), component: Projects, canActivate: [AdminAuthGuard] },
...
{ path: '**', component: LoginForm },
];
而在/app/common/url.ts我有類似的東西(我做下面的代碼在這裏用手工不檢查的話):
export class Url {
map = {
'login': 'my-super-login',
'home': 'my-link-home',
'clients': 'favourite-clients',
'projects': 'bets-projects',
}
public static to(routingName : string) {
return this.map[routingName];
}
}
而在你的項目中每一個地方在林ks你還必須使用Url.to(...)
方法(在控制器中使方法links(route)
調用Url.to,並在模板中使用它...)。 上面的Url靜態類是理論上的。在實踐中我用戶polyglot.js小型圖書館有參數和翻譯URL支持 - 所以我Url.to方法看起來就像這樣:
export class Url {
public static to(routingName : string, values?:any) {
return Lang.t('routing.'+routingName, values);
}
}
其中用類:
var Polyglot = require('../../../../node_modules/node-polyglot/build/polyglot.min.js');
import { Auth } from '../';
import { en } from './en';
import { pl } from './pl';
export class Lang {
private static instance = null;
public polyglot:any;
private static emergencyLang = 'en';
constructor() {
this.polyglot = new Polyglot();
this.polyglot.extend({
en,
pl,
});
if(Lang.instance) return;
Lang.instance = this;
}
public static t(key:string, values?:any) {
let self = Lang.getInstance();
let user = Auth.user();
let userLang = user ? user.lang : (navigator.language || navigator.userLanguage);
let langKey = userLang + '.'+key;
if(self.polyglot.has(langKey)) return self.polyglot.t(langKey, values);
return self.polyglot.t(Lang.emergencyLang + '.'+key, values);
}
private static getInstance() {
if(Lang.instance == null) Lang.instance = new Lang();
return Lang.instance;
}
}
例如英寸/en.ts我有:
export const en = {
routing : {
login: 'login',
clients: 'my-clients',
projects: 'my-projects',
"project.pages": 'my-projects/%{project_id}/pages',
...
}
login : "Login"
....
}
和其他語言類似。
===編輯:AOT支持===
我注意到aboce溶液(URL類)不與AOT合作。在angular-starter使用命令npm run build:aot
我們會看到以下錯誤:
Error: Error encountered resolving symbol values statically. Calling function 'Url', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol ROUTES in...
因爲AOT編譯app.routes.ts文件使用metadata js subset編譯。所以,下面我給的解決方案命名路由(帶參數)writen使用compatibile與AOT JS集:
export class Url {
public static to(routingName: string, values?: any) {
return {
'clients' : 'favourite-clients/' + values['client_id'],
'projects' : 'projects/' + values['project_id'] + '/best',
...
}[routingName];
}
}
可能是在app.routes.ts及以上Url.to方法使用一些技巧將是更多鈔票也對將multilang與AoT兼容。
你正在使用哪個版本的角? –
Angular2,package.json:「@ angular/router」:「2.0.0-rc.1」(「@ angular/core」:「2.0。0-RC.1" ) –
PS:https://docs.google.com/document/d/1WLSNV3V1AKdwLwRiLuN7JqbPBKQ_S5quRlcT5LPIldw/edit#可以幫助你 –