【问题标题】:Angular 2 Component not displayingAngular 2 组件不显示
【发布时间】:2016-07-13 06:51:16
【问题描述】:

我有以下组件,当我的路由器路由到该组件时,我会看到以下屏幕:

这是组件:

import { Component } from 'angular2/core';
import { Router } from 'angular2/router';
import { UserService } from '../services/user.service';

@Component({
  selector: 'login',
  template: 'client/dev/user/templates/login.html',
  styleUrls: ['client/dev/todo/styles/todo.css'],
  providers: []
})
export class LoginComponent {
  constructor(
    private userService: UserService, 
    private router: Router
  ) { }

  onSubmit(email, password) {
    this.userService.login(email, password).subscribe((result) => {
      if (result) {
        this.router.navigate(['Home']);
      }
    });
  }
}

为什么渲染不正确?

注意:如果我更改路由以使用不同的组件,它工作正常,所以我认为这是这个特定的组件,或者可能是一些依赖问题。

【问题讨论】:

  • 这里,解决方案是在 html 中使用正确的标签名称。选择器不是魔术的类名,而是selector中包含的字符串。

标签: angular angularjs-routing


【解决方案1】:

您需要在Component 装饰器中使用templateUrl 而不是template

@Component({
  selector: 'login',
  templateUrl: 'client/dev/user/templates/login.html', // <-----
  styleUrls: ['client/dev/todo/styles/todo.css'],
  providers: []
})
export class LoginComponent {
  (...)
}

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 2020-11-20
    • 2017-01-10
    • 1970-01-01
    • 2017-09-24
    • 2017-01-03
    • 2016-07-09
    • 2017-01-17
    相关资源
    最近更新 更多