0

I'm beginner for vuejs and typescript. I made a new window property for using a variable globally and got this error.

 Property 'getAlert' does not exist on type 'Window & typeof globalThis'.
    15 |     methods:{
    16 |       getAlert(text : string){
  > 17 |         window.getAlert(text)
       |                ^
    18 |       }

But when I run the page, it's working well. I don't understand this weird happening. How to solve this problem? I wanna delete this error. here is my code.

vue file

<template>
  <div class="home">
    <v-btn  @click="[getAlert('11111')]"></v-btn>
        <h1>This is an Home page</h1>
    <!-- <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js + TypeScript App"/> -->
  </div>
</template>

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
    name: 'Home',
    methods:{
      getAlert(text : string){
        //window.getAlert(text)
        Vue.prototype.$getAlert(text)

      }
    }
})
</script>

main.ts

import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import vuetify from './plugins/vuetify'

Vue.config.productionTip = false

var vm = new Vue({ 
  router,
  store,
  vuetify,
  methods:{
    getAlert(text :string){
         alert(text)
       }
  },
  render: h => h(App)
}).$mount('#app')
Vue.prototype.$getAlert = vm.getAlert

.d.ts file

declare global{ 
  interface Window{
    getAlert : any;
  }
}
Charles
  • 181
  • 1
  • 14
  • [Don't use `window` for global variables](https://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice) – Dan Jan 08 '21 at 03:48
  • is there any alternative way? I also tried using Vue.prototype. – Charles Jan 08 '21 at 04:44
  • Use Vuex for global state data/methods. You can use `Vue.prototype` as well, especially if what is being stored doesn't need to be reactive. You could also use helper/utility modules in that case. – Dan Jan 08 '21 at 04:54
  • I used Vue.prototype, and it's workng at web and not working at webview. But using window is working at webview. could you answer about this? I updated the code – Charles Jan 08 '21 at 05:43
  • For `Vue.prototype` what's the error – Dan Jan 08 '21 at 11:02

0 Answers0