将前缀模式标志传递给Angular 2 params

时间:2016-12-12 15:56:52

标签: angular angular2-routing

我有一个Angular 2应用程序,并希望传入一个带有true / false布尔标志的模式,可以由app组件读取,如下所示:

http://www.outapp.com/<appbase>/<mode>/<routes>/<child-routes>/<etc>

@Component({
  selector: 'my-app', // <my-app></my-app>
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  standAloneMode: boolean = true;

  // private viewContainerRef: ViewContainerRef;
  constructor(private router: Router, private api: ApiService, private loader: LoaderService) {
    standAloneMode = Router.SOMETHINGFROMTHEPARAMS;

  }

如何在不干扰已配置路线的情况下传递旗帜?

1 个答案:

答案 0 :(得分:-1)

您可以使用route.ts中的以下路径参数:

const routes; Routes = [
    ...
    {path: '/:mode/routes/child-routes/etc', component: YourComponent}
    ....
]

然后在你的组件中,阅读模式参数:

ngOnInit() {
    this.route.params.subscribe(param=> {
          if (param['mode'] !== undefined) {
            this.mode= param['mode'];
            etc.
          }
    });
    etc.
}