nuxtjs判断前端渲染或者禁止后端渲染

2020-02-20 18:34:07

有时候后端渲染会出现问题,我们要屏蔽掉后端渲染。

js代码判断前后端


Nuxt v1.4.0文档中的官方指南如下:https://nuxtjs.org/faq/window-document-undefined#window-or-document-undefined-

如果需要指定仅在客户端导入资源,则需要使用process.browser变量。

例如,在.vue文件中:

if (process.browser) {
  require('external_library')}

如果在多个文件中使用此库,我们建议通过nuxt.config.js将其添加到供应商包中:

build: {
  vendor: ['external_library']}


html代码判断前后端,可参考nuxtjs文档,https://nuxtjs.org/api/components-client-only/


<template>  <div>    <sidebar />    <client-only placeholder="Loading...">      <!-- this component will only be rendered on client-side -->      <comments />    </client-only>  </div></template>


有时候import引入的组件会自动运行,导致后端渲染崩溃,我们要采取动态的加载import来解决这个问题。

我采取了下面这个方法。

var name = 'system';
var myComponent =() => import('../components/' + name + '.vue');
var route={
  name:name,
  component:myComponent


  • 2017-12-25 11:07:58

    mogodb 备份

    error connecting to db server: server returned error on SASL authentication

  • 2017-12-25 15:53:15

    MongoDB3.4版本配置详解

    配置文件方面,mongod和mongos有很多相同之处,下文中如有区别之处将会特别指出。 在一个节点上,通常同时启动mongod和mongos进程是正常的,他们之间不存在资源竞争,但是为了避免冲突,我们希望它们使用各自的配置文件,比如mongod.conf、mongos.conf;mongodb的某些平台下的安装包中没有自带配置文件,需要开发者自己创建。