Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
  }
}
question from:https://stackoverflow.com/questions/65623056/vue-js-global-variable-got-error-but-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
177 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...