字符串包含无效字符?

String contains an invalid character?

我正在尝试创建一个简单的用户登录和密码页面,目前没有后端服务。

输入密码后,我想将我的页面路由到仪表板。我正在使用核心 UI 框架和 Angular 4。现在我有一个未捕获的错误。我该如何解决以下错误?

login.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { DashboardComponent } from '../dashboard/dashboard.component';

declare var Dashboard:any;

@Component({
    templateUrl: 'login.component.html',
})
export class LoginComponent {
    data: any = {};
    constructor(private router: Router){}
    formSubmit(){
      if(this.data.username == "admin" && this.data.password == "admin"){
        this.router.navigate([Dashboard]);
      }
    } 
}

login.component.html

<div class="app flex-row align-items-center" (ngSubmit)="formSubmit()">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card-group mb-0">
<div class="card p-2">
   <div class="card-block">
      <h1>Login</h1>
      <p class="text-muted">Sign In to your account</p>
      <div class="input-group mb-1">
         <span class="input-group-addon"><i class="icon-user"></i></span>
         <input type="text" class="form-control" placeholder="Username" [(ngModel)="data.username"]>
      </div>
      <div class="input-group mb-2">
         <span class="input-group-addon"><i class="icon-lock"></i></span>
         <input type="password" class="form-control" placeholder="Password" [(ngModel)="data.password" >
      </div>
      <div class="row">
         <div class="col-6">
            <button type="button" class="btn btn-primary px-2">Login</button>
         </div>
      </div>
   </div>
</div>
</div>
</div>
</div>
</div>
</div>

您需要定义属性的类型。 Object 不包含您的属性:

interface YourDataType {
    username?: string;
    password?: string;
}

export class LoginComponent {
    data: YourDataType = {};
    // ....
    formSubmit() {
        if (this.data.username == "admin" && this.data.password == "admin") { // No error
        }
    }
}

或者如果您不想定义类型,您可以通过将类型指定为 any:

来选择退出
export class LoginComponent {
    data: any = {};
    // ....
    formSubmit() {
        if (this.data.username == "admin" && this.data.password == "admin") { // No error
        }
    }
}

在你的例子中,我在你的模板中发现了这样的错字:<input type="password" class="form-control" placeholder="Password" [(ngModel)="data.password" > with ] missed at the line before the >

我遇到了类似的问题,一切都编译得很好,但在运行时失败了。