所以我有这个简单的组件:
import {
Component,
ViewEncapsulation,
OnInit,
} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'custom-element',
template: '<div>{{ chuckText$ | async }}</div>',
styleUrls: ['./button.component.css'],
encapsulation: ViewEncapsulation.Native
})
export class ButtonComponent implements OnInit {
public chuckText$ = new BehaviorSubject<string>('');
public constructor(
private http: HttpClient
) {
this.chuckText$.next('Default Text');
}
ngOnInit() {
// nothing too fancy, just for testing purpose
this.http.get('https://api.chucknorris.io/jokes/random')
.subscribe((data: any) => {
console.log(data.value);
this.chuckText$.next(data.value);
});
}
}
在构建我的组件并将其添加到一个简单的 angularJS 应用程序之后,(我需要向旧应用程序添加一个自定义元素)我在控制台中获得了带有默认文本和 http.get 结果的元素,但我的组件还没有更新了,如图。
我显然错过了一些对我来说并不明显的东西。关于为什么内容不更新的任何想法?提醒一下,此功能已发布但处于实验状态,也许就是这样。
作为参考,这是我的 app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { HttpClientModule } from '@angular/common/http';
import { ButtonComponent } from './button/button.component';
@NgModule({
declarations: [ButtonComponent],
imports: [BrowserModule, HttpClientModule],
entryComponents: [ButtonComponent],
})
export class AppModule {
constructor(private injector: Injector) {
const customElement = createCustomElement(ButtonComponent, { injector });
customElements.define('custom-button', customElement);
}
ngDoBootstrap() { }
}