【问题标题】:Why the onclick function not working in html -angular为什么 onclick 函数在 html -angular 中不起作用
【发布时间】:2021-09-29 15:10:23
【问题描述】:

user.html:

<div class="bg-image" 
  style="background-image: url('./assets/Images/Homepage.jpg');
         height:100vh; position:fixed; min-width:100%; min-height:100%; background-repeat:no-repeat; background-size:cover">
         <mat-toolbar style="background-color:purple;" >
            <mat-toolbar-row>
             <span style="color:white; font-size:medium;padding:20px;margin-top:15px;">HOME</span>
             <span style="color:white; font-size:medium;padding:20px;margin-top:15px;cursor: pointer;" (click)="About()">ABOUT</span>
             <span style="color:white; font-size:medium;padding:20px;margin-top:15px;">CLASSES</span>
             <span style="color:white; font-size:medium;padding:20px;margin-top:15px;">CONTACT US</span>
            </mat-toolbar-row>
         </mat-toolbar>
</div>

user.ts:

import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {

  constructor(private router:Router) { 

  }

  ngOnInit(): void {
  }

  About()
  {
    this.router.navigate(['/about-page']);
  }

}

单击关于它不会导航到关于页面。在我的关于页面组件中,我给出了 1 行 html,因为它可以正常工作。

【问题讨论】:

  • 控制台有错误吗?
  • 路由是如何配置的?
  • 没有错误,当我点击什么都没有发生时
  • 您的路线似乎配置不正确。你能告诉我们你的路由模块吗?
  • 看到一个是“路由器路径”,另一个是“组件名称”,另一个是“组件的选择器”

标签: html angular typescript angular-material uinavigationbar


【解决方案1】:

尝试导入RouterModule

//app.module.ts
import {RouterModule} from '@angular/router';

@NgModule({
imports: [RouterModule]
});

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2020-05-02
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多