我有一个项目可以在Gmail Chrome扩展程序中插入角元素,以便在inboxSDK
中注入我使用的元素(也可以使用vanilla JS制作)。
问题是输入keyup
事件中的意外行为以及角度材料无法正常工作。
例如,<mat-menu>
仅在第二次点击时打开,并且输入时未在焦点上应用matInput
指令动画。 keyup
事件的另一个有线问题是某些键未显示在输入上。我看到事件发生但输入中的文本在我检查时没有改变(空格和v等),但是当我检查普通html中的angular元素时,一切都按预期工作。在gmail chrome扩展程序中有什么不同?
为了编译角度元素,我使用了这个practical introduction to web components with angular 6 blog post on the Medium website
中的指令附件是我的Angular元素代码:
import {
Component,
OnInit,
ViewEncapsulation,
AfterViewInit
} from '@angular/core';
@Component({
selector: 'custom-input',
templateUrl: './custom-input.component.html',
styleUrls: ['./custom-input.component.css'],
encapsulation: ViewEncapsulation.Native
})
export class CustomInputComponent implements OnInit, AfterViewInit {
public text: string = ' ';
constructor() {
console.log('construct');
}
ngOnInit() {
console.log('ngOnInit');
}
ngAfterViewInit() {
console.log('ngAfterViewInit')
}
onkeyUp(x) {
console.log(x);
this.text = x;
}
}
<mat-form-field class="example-full-width">
<input matInput (keyup)="onkeyUp($event.target.value)">
</mat-form-field>
<span style="color: blue;">{{text}}</span>
<!--this is new-->
<button mat-button [matMenuTriggerFor]="menu">Menu</button>
<mat-menu #menu="matMenu">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
</mat-menu>
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { AppComponent } from './app.component';
import { CustomInputComponent } from './custom-input/custom-input.component';
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import {MatButtonModule, MatInputModule, MatMenuModule} from "@angular/material";
@NgModule({
declarations: [
AppComponent,
CustomInputComponent
],
imports: [
BrowserAnimationsModule,
BrowserModule,
MatButtonModule,
MatMenuModule,
MatInputModule
],
providers: [],
entryComponents: [
CustomInputComponent
]
})
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap() {
const el = createCustomElement(CustomInputComponent, {injector: this.injector});
customElements.define('custom-input', el);
}
}
和扩展代码:
//manifest.json
{
"name": "angular elements",
"version": "1.0.0",
"manifest_version": 2,
"description": "angular elements",
"icons": {
"16": "images/16.png",
"128": "images/128.png"
},
"content_scripts": [
{
"run_at": "document_start",
"matches": [
"<all_urls>"
],
"js": [
"scripts/content.js"
]
}
],
"permissions": ["<all_urls>"]
}
//content.js
'use strict';
function injectFiles() {
let url = 'http://127.0.0.1:8080/styles.css';
let link = document.createElement("link");
link.href = url;
link.type = "text/css";
link.rel = "stylesheet";
document.getElementsByTagName("head")[0].appendChild(link);
}
function loadjs(file, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = file;
script.onload = function(){
callback();
};
document.body.appendChild(script);
}
document.addEventListener('DOMContentLoaded', () => {
injectFiles();
loadjs('http://127.0.0.1:8080/scripts/webcomponents-bundle.js', function(){
loadjs('http://127.0.0.1:8080/scripts/framework-poll.js', function(){
const div = document.createElement('div');
div.id = 'hello-div';
div.style.height = '400px';
div.style.zIndex = "99999999999";
div.style.width = '400px';
div.style.background = '#E9EBEE';
div.style.display = 'block';
div.style.position = 'absolute';
div.style.bottom = "-60px";
div.style.right = "0";
document.body.appendChild(div);
let elem = document.getElementById('hello-div');
elem.insertAdjacentHTML('beforeend', '<custom-input><custom-input/>')
});
});
});
根据我的检查,当我在没有inboxsdk的其他网站上使用扩展时,会发生输入事件发生的行为。双向绑定也停止工作。