V8Js :: compileString():9272:ReferenceError:未定义窗口

时间:2018-06-23 21:07:43

标签: laravel vue.js serverside-rendering v8js

    <?php

    namespace App\Http\Controllers\Auth;

    use Illuminate\Support\Str;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use App\Model\User;
    use Illuminate\Support\Facades\Auth;
    use Socialize;
    use Redirect;
    use Input;
    use Mail;
    use App\Mail\verifyEmail;
    use Carbon\Carbon;
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Support\Facades\File;

    class CustomAuthController extends Controller
    {
    // Register form

    private function render() {
        $renderer_source = File::get(base_path('node_modules/vue-server-renderer/basic.js'));
        $app_source = File::get(public_path('js/entry-server.js'));
        $v8 = new \V8Js();
        ob_start();
        $v8->executeString('var process = { env: { VUE_ENV: "server", NODE_ENV: "production" }}; this.global = { process: process };');
        $v8->executeString($renderer_source);
        $v8->executeString($app_source);
        return ob_get_clean();
      }
      public function get() {
        $ssr = $this->render();
        return view('app', ['ssr' => $ssr]);
      }
}

我遵循https://dzone.com/articles/server-side-rendering-with-laravel-amp-vuejs-25并安装了v8js。但是错误是$ v8-> executeString($ app_source);出现“ V8Js :: compileString():9272:ReferenceError:窗口未定义”的错误

如何解决此错误。我不知道

================================================ ==

更新我可以运行它。我通过删除在app.js中更改代码

/** after i delete it.i can run with server side rendering
  require('./bootstrap');
  require('./bulma-carousel.min')
  window.Vue = require('vue');
  can you tell me why ?*/



  import App from './components/App.vue';
  import VeeValidate from 'vee-validate';
  import Vue from 'vue';
  import router from './router'

  Vue.use(VeeValidate);
  /**
   * Next, we will create a fresh Vue application instance and attach it to
   * the page. Then, you may begin adding components to this application
   * or customize the JavaScript scaffolding to fit your unique needs.
   */

  Vue.component('index-component', require('./components/index.vue'));

  export function createApp() {
      return new Vue({
        render: h => h(App)
      });
  }

1 个答案:

答案 0 :(得分:4)

问题很简单。您请求的引导文件可能在某处使用window元素。同样在此行window.Vue = require('vue');上,您正在使用window全局对象。浏览器中可以使用Window作为全局对象,但是在服务器上运行的javascript没有window对象。因此错误。这就是我们需要为服务器和客户端制作单独的javascript文件的原因。尝试咨询您在其对服务器端呈现的支持上实现的软件包。

尝试逐个禁用各种导入,并找出破坏您对SSR的支持并进行修复或寻找替代方法。