Plugin only server side

◇◆丶佛笑我妖孽 提交于 2019-12-10 21:55:13

问题


I have a plugin that I don't want the client to see. Unfortunately it is allways built both for the server and the client. How can this be prevented?

<template>
    <div>
        Test
    </div>
</template>

<script>
    import getAll from '~/plugins/api'
    export default {
        asyncData (context, callback) {
            getAll(function(data){
                callback(null,data);
            })
        }
    }
</script>

This is my .vue file. The fetch of the data is working but i can also See the code from the client side which i don't want.


回答1:


Maybe you can use the context.isServer property.
It's a boolean to let you know if you're actually renderer from the server-side.

<script>
import getAll from '~/plugins/api'
export default {
    asyncData (context, callback) {
        if (context.isServer) {
            getAll(function(data){
                callback(null,data);
            })
        }
    }
}
</script>

More details about Nuxt context here: https://nuxtjs.org/api/context


update: (thanks @Lanayx)

"Note: Since Nuxt.js 2.4, mode has been introduced as option of plugins to specify plugin type, possible value are: client or server. ssr: false will be adapted to mode: 'client' and deprecated in next major release."

// nuxt.config.js:

export default {
  plugins: [
    { src: '~/plugins/both-sides.js' },
    { src: '~/plugins/client-only.js', mode: 'client' },
    { src: '~/plugins/server-only.js', mode: 'server' }
  ]
}


来源:https://stackoverflow.com/questions/45494579/plugin-only-server-side

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!