[英]Angular 6 routing using typescript function
I am trying to route programmatically through a function because I need a way around when clicking a link (a tag) inside an svg (since routerLink) does not work. 我试图以编程方式通过一个函数路由,因为我需要一种解决方法,当单击一个svg内的链接(标签)(因为routerLink)不起作用。
So far this is what I have: 到目前为止,这就是我所拥有的:
<g id="MenuStub4">
<path
id="box"
fill="#231F20"
d="M1171.088,420.473c0,4.307-4.387,7.799-9.795,7.799h-94.15c-5.408,0-9.793-3.492-9.793-7.799v-8.936
c0-4.307,4.385-7.799,9.793-7.799h94.15c5.408,0,9.795,3.492,9.795,7.799V420.473z"
/>
<text
transform="matrix(1.3982 0 0 1 1096.8799 419.4614)"
font-size="11.582"
>
<a fill="white" (click)="navigate()">
Blog
</a>
</text>
</g>
And the ts file: 和ts文件:
import { Component } from "@angular/core";
import { Router } from "@angular/router";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
title = "BrainToBytes";
constructor(private router: Router) {}
navigate(): void {
this.router.navigateByUrl("./blog");
}
}
App-routing.module.ts: APP-routing.module.ts:
import { BlogComponent } from './blog/blog.component';
import { NgModule, Component } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [{
path: 'blog',
component: BlogComponent
}];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
I checked the path, even if I change it with "/blog" it still doesn't work 我检查了路径,即使我用“/ blog”更改它仍然无法正常工作
I am a noobie with angular, so I am not sure if I am setting up the routing correctly or something else is wrong. 我是一个有角度的noobie,所以我不确定我是否正确设置路由或其他错误。
if your app-routing.module.ts represents something like this: 如果你的app-routing.module.ts代表这样的事情:
const routes: Routes = [{ path: 'blog', component: BlogComponent }];
if you are navigating through typescript try this: 如果你正在浏览打字稿,试试这个:
this.router.navigate(['/blog']);
You cannot put a routerLink inside an svg, so don't try that. 你不能把一个routerLink放在svg里面,所以不要试试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.