无法在Angular 4 + Webpack中使用Virtual Keyboard jQuery插件

时间:2017-10-25 09:43:48

标签: javascript jquery angular webpack asp.net-core

我试图在Asp Core + webpack的Angular 4模板上使用虚拟键盘 我已经安装了npm install --save-dev @ types / virtual-keyboard 在webpack.config.vendor.js中我把这行:

const treeShakableModules = [
    .....
    'virtual-keyboard',
   ...
];

在我的组件模板中:

<input id="mycontrol" type="text">

in .ts:

import * as jQuery from "jquery";
import { KeyboardOptions, NavigateOptions } from "virtual-keyboard";

const kbOptions: KeyboardOptions = {
    display: {
        bksp: "\u2190",
        accept: `Next`,
        cancel: `Back`,
        normal: "ABC",
        meta1: "#+-",
        space: "Space",
        alt: `Alt`,
        s: `ABC`,
    },
    acceptValid: true,
    type: "input",
    layout: "custom",
    customLayout: {
        normal: [
            `a b c d e f g h i j k l m`,
            `n o p q r s t u v x y z w`,
            `1 2 3 4 5 6 7 8 9 0 . _ @`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
        shift: [
            `A B C D E F G H I J K L M`,
            `N O P Q R S T U V X Y Z W`,
            `1 2 3 4 5 6 7 8 9 0 . _ @`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
        meta1: [
            `- / : ; ( ) \u20ac & \" ! ? ' \``,
            `[ ] { } # % ^ * + = ° ´ §`,
            ` \\ | ~ < > $ \u00a3 \u00a5 , ' ² ³`,
            `{space} {meta1} {bksp}`,
            `{cancel}  {accept}`
        ],
        "alt-shift": [
            `A B C D E F G H I J K L M N O`,
            `P Q R S T U V X Y Z W \u00df \u00dc \u00d6 \u00c4`,
            `1 2 3 4 5 6 7 8 9 0 . _ @ \u0301`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
        alt: [
            `a b c d e f g h i j k l m n o`,
            `p q r s t u v x y z w \u00df \u00fc \u00f6 \u00e4`,
            `1 2 3 4 5 6 7 8 9 0 . _ @ \u0301`,
            `{alt} {s} {space} {meta1} {s} {bksp} `,
            `{cancel}  {accept}`
        ],
    },
    lockInput: true,
    alwaysOpen: true,
    appendLocally: true,
    color: "light",
    class: "sxcycx",
    updateOnChange: true,
    usePreview: false,
    tabNavigation: false,
    canceled: () => { console.log("cancelled"); }
};

const navOptions: NavigateOptions = {
    position: [0, 0],
    toggleMode: true,
    focusClass: "hasFocus",
    rowLooping: true,
};

然后

ngAfterViewInit(): any {
             try {



jQuery('#mycontrol').css("background-color", "red");  //I see the change
jQuery('#mycontrol').keyboard(kbOptions).addNavigation(navOptions);

....
在控制台中

没有错误,但聚焦文本框没有显示键盘 我认为必须要把东西放在webpack.config.vendor.js

感谢

1 个答案:

答案 0 :(得分:2)

通过npm安装jquery和虚拟键盘。试试这个:

// In *.ts file:
import * as $ from 'jquery';
import 'virtual-keyboard';

$('.vk').keyboard({
  change: (e, keyboard, el) => {
    // Dispatch input event to notify angular about changes
    const inputEvent: any = document.createEvent('CustomEvent');
    inputEvent.initEvent('input', true, true);
    el.dispatchEvent(inputEvent);
  }
});

//在scss中:

@import "../../node_modules/virtual-keyboard/dist/css/keyboard-dark.min";
顺便说一句,为了能够订阅虚拟键盘事件,最好在那里创建指令并初始化键盘。