简体   繁体   English

使用 jQuery 插件的 VueJS 2 服务器端渲染

[英]VueJS 2 server-side rendering with jQuery plugin

I'm trying to convert an old landing page that has a few jQuery plugins to a Vue app.我正在尝试将具有一些 jQuery 插件的旧登录页面转换为 Vue 应用程序。 The reason is, that the landing page is being extended and will become a web app.原因是,登陆页面正在扩展,将成为一个网络应用程序。

Using https://github.com/vuejs/vue-hackernews-2.0/ as a template.使用https://github.com/vuejs/vue-hackernews-2.0/作为模板。 Main reason is that it does server side rendering.主要原因是它进行服务器端渲染。

Problem is the error:问题是错误:

ReferenceError: window is not defined
    at Object.<anonymous> (__vue_ssr_bundle__:3398:2408)
    at __webpack_require__ (__vue_ssr_bundle__:21:30)
    at Object.module.exports.Object.defineProperty.value (__vue_ssr_bundle__:1280:126)
    at __webpack_require__ (__vue_ssr_bundle__:21:30)
    at Object.<anonymous> (__vue_ssr_bundle__:2099:3)
    at __webpack_require__ (__vue_ssr_bundle__:21:30)
    at Object.<anonymous> (__vue_ssr_bundle__:1380:81)
    at __webpack_require__ (__vue_ssr_bundle__:21:30)
    at Object.module.exports.Object.defineProperty.value (__vue_ssr_bundle__:378:66)
    at __webpack_require__ (__vue_ssr_bundle__:21:30)

So far, I've found out that the problem is caused by window not being in Node.到目前为止,我发现问题是由于window不在 Node.js 中引起的。 However, I do not know how to make it available or how I should inject jQuery after the page is server side rendered.但是,我不知道如何使它可用或者在页面被服务器端渲染后我应该如何注入 jQuery。

vue-hackernews-2.0 is almost unchanged. vue-hackernews-2.0 几乎没有变化。 The main change I did was adding jQuery to webpack.client.config.js .我所做的主要更改是将 jQuery 添加到webpack.client.config.js

This is added to plugins (found another answer that it should make jQuery available when it's needed in the app):这被添加到plugins中(找到另一个答案,它应该在应用程序中需要它时使 jQuery 可用):

new webpack.ProvidePlugin({
  $: 'jquery',
  jquery: 'jquery',
  'window.jQuery': 'jquery',
  jQuery: 'jquery'
})

And this is the LandingView.vue这是LandingView.vue

<template>
  <div class="landing-page">
  </div>
</template>

<script>
  import owl from '../../public/assets/libs/owl.carousel.2.0.0-beta.2.4/owl.carousel.min.js'
  import $ from 'jquery';

  export default {
    name: 'landing-view',
    created () {
      console.log('hello world!');
    }
  }
</script>

It turns out I needed to inject my jQuery plugin on mounted event https://v2.vuejs.org/v2/api/?#mounted事实证明我需要在mounted事件上注入我的 jQuery 插件https://v2.vuejs.org/v2/api/?#mounted

It also says它还说

This hook is not called during server-side rendering.在服务器端渲染期间不会调用此挂钩。

I just assigned the plugin to window and then I was able to initiate it.我刚刚将插件分配给window ,然后我就可以启动它了。

<template>
  <div class="landing-page">
  </div>
</template>

<script>
  // import $ from 'jquery'; //no need to import it, as it's provided by webpack

  export default {
    name: 'landing-view',
    mounted() {
      // created() is only available in v1

      window.owl = require('../../public/assets/libs/owl.carousel.2.0.0-beta.2.4/owl.carousel.min.js');


      // the rest of my code
      $('.some-carousel').owlCarousel();

    }
  }
</script>

I am not sure if this is the proper way to solve this.我不确定这是否是解决此问题的正确方法。 I appreciate any comments on best practices!感谢您对最佳实践的任何评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM