角度,定义导航组件上的全局变量



我有以下导航链接:

<a (click)="mylink(someLink)">SomeLink</a>

属于:

<div #someLink class="someLink active">
Container Content
</div>

但相对的TypeScript代码在相同的组件中只能在本地工作。但我希望将自己的导航组件和另一个内容组件中的链接外包。

mylink(page: any, event: any) {
page.classList.add('active'); // show the 'WebContainer SomeLink'
}

我怎么能意识到这一点?

您可以为此使用角度路由。请参阅:https://angular.io/tutorial/toh-pt5

生成应用程序时,只需说";是";到路由。在您的导航组件中构建如下链接:

<a [routerLink]="['yourcomponent']" ">SomeLink</a>

提示:不要使用href标记!!!

在app.modules.ts中检查:

import { AppRoutingModule } from './app-routing.module';

在app-routing.modules.ts中导入所有可点击组件:

import { YourComponent } from './yourcomponent/yourcomponent.component';

并将您的路线(例如链接(添加到相同的文件中,如下所示:

const routes: Routes = [
{ path: '', component: YourStartComponent },
{ path: 'yourcomponent', component: YourComponent },
];