我目前正在从Vue升级我的应用程序.JS1到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

---编辑---

main.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>

Main component:

<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>

推荐答案

这和埃斯林特有关.这样说:

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

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

Vue.js相关问答推荐

Vue路由路径匹配行为

Vue3组合-如何在组件卸载时注销回调

如何将$validator实例注入到使用组合API语法的Vue2.7应用程序中

在 Vue 中删除自定义组件代码重复的方法?

混合使用 React 和 Vue 是个好主意吗?

Vue CLI - 编译后将配置文件保留为外部

运行vue-cli-service build --watch时没有 CSS 文件

在 Vue 项目之间共享assets和组件

vuejs中watch方法和计算方法有什么区别

在 vuejs 中的图像中包含 router-link 标记

使用 aria-live 在 vue.js 中向屏幕阅读器宣布信息

VueJS + VueRouter:有条件地禁用路由

使用 Blade/Mustache 模板混合 Laravel 和 Vue.Js

JavaScript/VueJS:判断数组是否包含具有特定值元素的对象

Vue 组件 Vue-Instant

如何从 v-for 创建的对象中绑定多个类?

从 vue.js 中的 store 获得的计算(computed)属性的设置器

过渡后的 v-if 下方的过渡元素

Vue.js 的 $emit 和 $dispatch 有什么区别?

Vue-meta:metaInfo 无权访问计算(computed)属性