【问题标题】:'window' is not defined in Vue.js 2Vue.js 2 中未定义“窗口”
【发布时间】:2017-04-04 02:22:41
【问题描述】:

我目前正在将我的应用程序从 Vue.js 1 升级到 Vue.js 2。我的主要组件中的以下函数存在问题:

<script>
  export default {
    ready: function listenKeyUp() {
      window.addEventListener('keyup', this.handleKeyUp());
    },

    methods: {
      handleKeyUp(e) {
        if (e.keyCode === 27) {
          this.$router.go({ name: '/' });
        }
      },
    },
  };
</script>

我的控制台显示此错误:'window' is not defined 这怎么可能?我不明白原因。如何解决这个问题,为什么新版本会出现这个问题?

--- 编辑 --- 一些额外的代码:

ma​​in.js:

// Import plugins
import Vue from 'vue';
import VueResource from 'vue-resource';
import VueI18n from 'vue-i18n';

// Import mixins
import api from './mixins/api';

// Import router config
import router from './router';


// Register plugins
Vue.use(VueResource);
Vue.use(VueI18n);
Vue.mixin(api);


// Go
new Vue({
  router,
}).$mount('#app');

index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>New website</title>

        <link rel="shortcut icon" href="/static/favicon.ico" />
        <link rel="apple-touch-icon" href="/static/mobile.png">

        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato"> 
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    </head>
    <body>
        <div id="app">
            <router-view></router-view>
        </div>

        <noscript>
            <div class="container">
                <div class="col-sm-6 col-sm-offset-3">
                    <div class="alert alert-danger">
                        JavaScript is disabled in your web browser!
                    </div>
                </div>
            </div>
        </noscript>

    </body>
</html>

主要组件:

<template>
    <div>

        <div class="container">
            <header>
                HEADER HERE
            </header>
        </div>


        <div id="modal" v-if=" $route.name !== 'home' " transition="modal">
            <div id="modal-bg" :to="{ name: 'home' }"></div>
            <div id="modal-container">
                <div id="modal-header">
                    <h2>Modal</h2>
                    <router-link id="modal-close" :to="{ name: 'home' }">X</router-link>
                </div>
                <router-view></router-view>
            </div>
        </div>


        <nav id="primary-navigation">
            <div class="container-fluid">
                <div class="row">
                    NAV HERE
                </div>
            </div>
        </nav>

    </div>
</template>


<script>
  /* SCRIPT PART, SEE TOP OF THIS POST */
</script>


<style lang="scss">
  /* CSS */
</style>

【问题讨论】:

    标签: javascript window undefined vue.js vuejs2


    【解决方案1】:

    使用浏览器引用最安全的地方是mounted() 生命周期挂钩。特别是如果您使用像 Nuxt.js 或类似的 SSR 设置。

    【讨论】:

    • 正在使用 Nuxt,但在 created() 中不起作用,但在 mounted() 中起作用。
    【解决方案2】:

    这与 Eslint 有关。把这个:

    "env": {
        "browser": true,
        "node": true
    }
    

    在我的根目录中的.eslintrc.js 内解决了这个问题。 (source)

    【讨论】:

      【解决方案3】:

      尝试将侦听器放入您的 created() 方法中

      您还将丢失 this 的上下文,因此请使用词汇粗箭头来保留上下文

      // rest of export
      created() {
        // make an event listener and pass the right `this` through
        window.addEventListener('keyup', (event) => {
          // if the key is escape
          if (event.keyCode === 27) {
            // due to `=>` this is the this you're expecting
            this.keyHandler()
          }
        }
      },
      methods: {
        keyHandler() {
          // this *should* be the right this
          this.$router.go({ name: '/' })
        }
      }
      // rest of export
      

      完全未经测试,但它应该可以工作(vue 2.x)

      【讨论】:

      • 谢谢。尝试了您的代码,但不幸的是 NPM 控制台仍然显示:'window' is not defined。这怎么可能?可能是什么问题?
      • npm 控制台?这不是在浏览器中运行的吗?
      • 不,在控制台中输入“npm run dev”之后。
      • 发现它与 eslint 有关,请参阅我的回答。无论如何感谢您的帮助!
      【解决方案4】:

      通过编辑(创建)vue.config.js 修复 vue-cli 3.x 项目:

      module.exports = {
          configureWebpack: config => {
              config.output.globalObject = "this"
          }
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-01
        • 2019-12-02
        • 2019-07-01
        • 2021-10-06
        • 2019-06-03
        • 2021-01-15
        • 2019-10-12
        相关资源
        最近更新 更多