New to Vue I am trying to make a simple game. In my vue3 component in the template I use the component. And it works:)
<input
:value="input"
class="input"
@input="onInputChange"
placeholder="Tap on the virtual keyboard to start"
>
<SimpleKeyboard
ref="simplekeyboard"
@onChange="onChange"
@onKeyPress="onKeyPress"
:input="input"
:layout="layout"
></SimpleKeyboard>
Now I want to acces the clearInput() function, and set a custom Layout according to documentation.
The layout is defined in the gamecomponent. Also I have a method in the game-component checking if a player has entered the right description. It wordks like a charm but the input variable in the Simple-Keyboard is not cleared
_match(){
if(this.showCard.description.toUpperCase() === this.input.toUpperCase()){
this.matches++;
this.input='';
this.simpleKeyboard.clearInput();
}
Gives an error:
Uncaught TypeError: Cannot read properties of undefined (reading 'clearInput')
I do not know how to get in contact with the simple-keyboard component. I tried:
mounted() {
this.simpleKeyboard = this.$refs.simplekeyboard;
},
data() {
return{
simpleKeyboard:{},
}
When I check the simple-keyboard component in the Vue-devtools I read:
keyboard:Unknown Component
Strange I think, since the keyboard works fine, albeit in the default settings. But apparently there is something I do not understand yet. Can anyone point me to a solution on how to get to the settings of the simple-keyboard component instead of only being able to read the output?
The SimpleKeyboard component itself looks like:
<template>
<div class="simple-keyboard"></div>
</template>
<script>
import Keyboard from "simple-keyboard";
import "simple-keyboard/build/css/index.css";
export default {
name: "SimpleKeyboard",
data: () => ({
keyboard: null
}),
mounted() {
console.log("Before new Keyboard()");
this.keyboard = new Keyboard({
onChange: this.onChange,
onKeyPress: this.onKeyPress
});
console.log("After new Keyboard()");
},
methods: {
onChange(input) {
this.$emit("onChange", input);
},
onKeyPress(button) {
this.$emit("onKeyPress", button);
/**
* If you want to handle the shift and caps lock buttons
*/
if (button === "{shift}" || button === "{lock}") this.handleShift();
},
handleShift() {
let currentLayout = this.keyboard.options.layoutName;
let shiftToggle = currentLayout === "default" ? "shift" : "default";
this.keyboard.setOptions({
layoutName: shiftToggle
});
}
You did not add the add the component to your data, that's why you cannot access it.
You can use Vue.component()