首页 > 解决方案 > 带角度的 Chrome 扩展:无法使用 chrome API 初始化字段

问题描述

我正在尝试用 Angular 编写一个 chrome 扩展。我为弹出窗口编写了以下组件

@Component({
  selector: 'app-root',
  template: ` Current tab: {{ currentTab || '(empty)' }} `,
  styles: []
})
export class AppComponent implements OnInit {
  currentTab: string;

  ngOnInit(): void {
    this.getCurrentTabTitle().subscribe(title => this.currentTab = title);
  }

  getCurrentTabTitle() {
    return new Observable<string>(subscriber => {
      chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
        subscriber.next(tabs[0].title);
      });
    });
  }
}

tabs.query()确实调用了回调,但内容仍然存在"Current tab: (empty)"。为什么会这样?

标签: angulargoogle-chrome-extension

解决方案


推荐阅读