Javascript С;如果使用Typescript,则从VueJS中的子组件调用父方法
父组件具有方法 layoutMain.vueJavascript С;如果使用Typescript,则从VueJS中的子组件调用父方法,javascript,typescript,vue.js,nuxtjs,Javascript,Typescript,Vue.js,Nuxtjs,父组件具有方法 layoutMain.vue <script lang="ts"> .... hideRightSidebar() { document.body.classList.remove('right-bar-enabled') } .... </script> <template> <div id="wrapper"> .... </div>
<script lang="ts">
....
hideRightSidebar() {
document.body.classList.remove('right-bar-enabled')
}
....
</script>
<template>
<div id="wrapper">
....
</div>
<Rightbar />
</div>
</template>
如何解决这个问题
我还有一个关于访问i18n插件的问题
在nuxt.config.js中,我有一个设置:
nuxt.config.js
....
i18n: {
locales: ["en", "fr", "es", "ar"],
defaultLocale: "en",
vueI18n: {
fallbackLocale: "en",
messages: {
en: require("./locales/en.json"),
fr: require("./locales/fr.json")
}
}
}
....
如果我不使用TS,它可以正常工作
<script>
export default {
data() {
return {
....
current_language: this.$i18n.locale
};
},
....
导出默认值{
数据(){
返回{
....
当前语言:this.$i18n.locale
};
},
....
但是如果我使用TS,我就有错误
TypeError: Cannot read property '$parent' of undefined
current_language: this.$i18n(<-- error HERE).locale
Property '$i18n' does not exist on type 'Readonly<Record<never, any>> & Vue'
current_language:this.$i18n(这是否回答了您的问题?我不确定自己是否可以使用TS调用正确的$emit,因为我得到一个错误{无法读取未定义的属性'$emit',因为我还没有学会如何正确调用$emit来从子级发送事件,并在父级中处理它,这通常比直接从子级->父级进行通信更好,因为Vue的方法是单向数据流。它可以防止子级错误地改变父级的状态。我建议学习如何发送事件。@MattU我知道这一点,问题是我想将项目转换为Typescript,但我不知道如何使用TS syntaxI来执行此操作。了解如何在TS中执行此操作。
<script>
export default {
data() {
return {
....
current_language: this.$i18n.locale
};
},
....
current_language: this.$i18n(<-- error HERE).locale
Property '$i18n' does not exist on type 'Readonly<Record<never, any>> & Vue'