17

我目前正在将我的应用程序从 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 这怎么可能?我不明白原因。如何解决这个问题,为什么新版本会出现这个问题?

--- 编辑 --- 一些附加代码:

主.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');

索引.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>
4

4 回答 4

21

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

于 2018-03-13T18:21:00.310 回答
6

它与埃斯林特有关。把这个:

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

.eslintrc.js在我的根目录里面解决了这个问题。(来源

于 2016-11-21T20:10:00.960 回答
4

尝试将侦听器放入您的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)

于 2016-11-21T11:09:05.487 回答
0

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

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

于 2018-09-17T09:46:14.820 回答