有时候后端渲染会出现问题,我们要屏蔽掉后端渲染。
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