简体   繁体   English

Angular:加载动态组件

[英]Angular: Load dynamic component

I have to load components in the routes dynamically. 我必须动态加载路由中的组件。

There is an environment variable in the env file and depending upon the value of that env variable I need to define component used in a route. env文件中有一个环境变量,根据该env变量的值,我需要定义路径中使用的组件。

Example: 例:


const mydashboard= env.value==='EnvA'?'dashboardA':'dashboardB;

const routes: Routes = [
    { path: '/dashboard', component: myDashboard }
];

Please let me know how can I make some of my routes dynamic as shown in the above examples which depend on some env variable value. 请让我知道如何使我的一些路线动态,如上面的示例所示,这取决于某些env变量值。

Try this: 尝试这个:

import { DashboardAComponent } from './dashboard-a/dashboard-a.component';
import { DashboardBComponent } from './dashboard-b/dashboard-b.component';

.
.
.

const routes: Routes = [
];

if (env.value === 'EnvA') {
  routes.push({ path: 'dashboard', component: DashboardAComponent });
} else if (env.value === 'EnvB') {
  routes.push({ path: 'dashboard', component: DashboardBComponent });
}

Basically you add new route object into routes array depending on your env.value. 基本上,您可以根据env.value将新路由对象添加到routes数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM