简体   繁体   English

在使用Nuxt进行SSR期间未调用Vue实例`watcher'

[英]Vue instance `watcher` not called during SSR with Nuxt

I have a plugin with the following code: 我有一个带有以下代码的插件:

   let vm = new Vue({
      data: {
        foo: 1,
      },
      watch: {
        $data: {
          deep: true,
          handler(){
            console.log('changed')
          }
        }
      }
    })

    vm.foo = 123; // should trigger watcher

The problem is that the message 'changed' is printed only clientside, meaning that the watcher is not called serverside during SSR? 问题是'changed'消息仅在客户端打印,这意味着在SSR期间观察者未称为服务器端吗? Any explanation to this behavior? 对这种行为有任何解释吗? Thanks! 谢谢!

Here is a small implementation with pure Vue SSR: https://codesandbox.io/s/oqx461ll8z 这是一个纯Vue SSR的小型实现: https : //codesandbox.io/s/oqx461ll8z

The result is a bit different than expected. 结果与预期有所不同。 The watcher will drigger after the SSR process with the latest value. SSR流程完成后,观察者将使用最新值进行拖动。 As there is no reactivity on the server and Vue has to prefetch data for consistency and perf reasons ( source ), this what will happen. 由于服务器上没有反应性,Vue出于一致性和性能方面的原因( )必须预取数据,这将发生。

Result: 结果:

<div data-server-rendered="true">123</div>
{ foo: [Getter/Setter] } 123 123
changed

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

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