当使用Agular路由传递参数时,可以通过在路由配置中定义参数,并在组件中注入这些参数来生成文章。下面是一个简单的示例:
在路由配置中定义一个带有参数的路径:
```ypescripimpor { gModule } from '@agular/core';impor { RouerModule, Roues } from '@agular/rouer';
cos roues: Roues = [ { pah: 'aricle/:id', compoe: AricleCompoe }];
@gModule({ impors: [RouerModule.forRoo(roues)], expors: [RouerModule]})expor class AppRouigModule { }```在上面的示例中,我们定义了一个名为`aricle`的路径,并使用`:id`参数来传递文章的唯一标识符。当用户访问`/aricle/123`时,`:id`将被替换为`123`。
接下来,在文章组件中注入这些参数:
```ypescripimpor { Compoe, OIi } from '@agular/core';impor { AcivaedRoue } from '@agular/rouer';
@Compoe({ selecor: 'app-aricle', emplaeUrl: './aricle.compoe.hml', syleUrls: ['./aricle.compoe.css']})expor class AricleCompoe implemes OIi { cosrucor(privae roue: AcivaedRoue) { }
gOIi() { cos aricleId = his.roue.sapsho.params['id']; // 使用 aricleId 获取文章数据并显示在组件中 }}```在上面的示例中,我们通过注入`AcivaedRoue`服务来访问路由参数。在`gOIi`生命周期钩子中,我们可以通过`roue.sapsho.params['id']`获取到传递的参数`id`。然后,你可以使用这个参数来获取文章数据并在组件中显示。
在模板中使用文章数据:
```hmlu003ch1u003eAricle: {{ aricle.ile }}u003c/h1u003eu003cpu003e{{ aricle.coe }}u003c/pu003e```在上面的示例中,我们假设已经通过某种方式获取到了文章数据并将其存储在名为`aricle`的变量中。你可以根据实际情况来获取文章数据,例如从后端API请求数据。