Javascript 如何从类中装饰构件?
我想从angular4应用程序中的类更改组件的装饰器 大概是这样的:Javascript 如何从类中装饰构件?,javascript,angular,typescript,angular-components,angular-decorator,Javascript,Angular,Typescript,Angular Components,Angular Decorator,我想从angular4应用程序中的类更改组件的装饰器 大概是这样的: import { Component, OnInit } from '@angular/core'; import { BooksService } from './../books.service'; @Component({ selector: 'users', templateUrl: this.template, styleUrls: ['./users.component.css'] }) expor
import { Component, OnInit } from '@angular/core';
import { BooksService } from './../books.service';
@Component({
selector: 'users',
templateUrl: this.template,
styleUrls: ['./users.component.css']
})
export class UsersComponent {
users: any;
template: any = './users.component.html';
constructor(private booksService : BooksService) {
this.users = this.booksService.getItems();
}
}
我如何实现这一点 装饰器不是类的一部分,因此装饰器内的
this
不引用组件
从技术上讲,可以使用静态类来实现这一点:
@Component({
selector: 'users',
templateUrl: UsersComponent.template,
styleUrls: ['./users.component.css']
})
export class UsersComponent {
users: any;
public static template: any = './users.component.html';
}
…但这将完全无用,因为静态值与硬编码相同。
谢天谢地,Angular真的不需要在装饰器中使用任何变量。反正也不行;它在后台使用组件编译这些模板,更改模板会破坏编译器(尤其是AOT编译器)
您是否正在尝试更改组件的html?使用路由器
或结构指令通常是一个更好的主意
为组件切换出模板的更直接的方法是使用直接访问其他组件的
TemplateRef
s并可以与视图交换它们的方法 您不能执行代码中的操作。但是,你应该看看这个: