【问题标题】:Angular 6 routing using typescript function使用打字稿功能的Angular 6路由
【发布时间】:2019-09-17 14:47:32
【问题描述】:

我正在尝试通过函数以编程方式进行路由,因为在单击 svg 中的链接(标签)时我需要一种解决方法(因为 routerLink)不起作用。

到目前为止,这是我所拥有的:

    <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>

还有 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:

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 { }

我检查了路径,即使我用“/blog”更改它仍然不起作用

我是一个有角度的菜鸟,所以我不确定我是否正确设置了路由或其他问题。

【问题讨论】:

    标签: javascript typescript svg routing angular6


    【解决方案1】:

    如果您的 app-routing.module.ts 代表如下内容:

    const routes: Routes = [{ path: 'blog', component: BlogComponent }];

    如果你正在浏览打字稿,试试这个:

    this.router.navigate(['/blog']);

    您不能将 routerLink 放在 svg 中,所以不要尝试。

    【讨论】:

    • 你的意思是app-routing.module.ts吗?我会在上面的问题中输入它的样子谢谢你的回答我认为你正在做某事
    • 我将它添加到应用程序路由中,[routerlink] 也无法在 svg 中工作“无法绑定到 'href',因为它不是 ':svg 的已知属性:a'。”
    • 好的,我把它路由了,实际链接到localhost:4200/blog,但是博客页面没有显示
    猜你喜欢
    • 2017-11-28
    • 2016-09-05
    • 1970-01-01
    • 2019-02-23
    • 2022-01-24
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2021-03-29
    相关资源
    最近更新 更多