为什么不能使用import通过包实例访问函数

为什么不能使用import通过包实例访问函数

本文介绍了为什么不能使用import通过包实例访问函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ES6导入,并且遇到了vue-server-renderer的问题. 与此问题类似但不太一样.

I am trying to use ES6 imports and am running into a problem with vue-server-renderer. It is similar to this question but not quite the same.

运行此程序时出现错误消息TypeError: Cannot read property 'createBundleRenderer' of undefined:

I get an error saying TypeError: Cannot read property 'createBundleRenderer' of undefined when I run this:

import vueServerRenderer from 'vue-server-renderer'
const createRenderer = (serverBundle) =>
    vueServerRenderer.createBundleRenderer(serverBundle, {
        runInNewContext: false,
        template: fs.readFileSync(path.resolve(__dirname, indexFolder,'index.html'), 'utf-8')
    });

但是,如果我执行以下操作,则没有错误:

But if I do the following, then no error:

import {createBundleRenderer} from 'vue-server-renderer'
const createRenderer = (serverBundle) =>
    createBundleRenderer(serverBundle, {
        runInNewContext: false,
        template: fs.readFileSync(path.resolve(__dirname, indexFolder,'index.html'), 'utf-8')
    });

为什么必须直接从vue-server-renderer导入功能?通过vueServerRenderer.createBundleRenderer()而不是createBundleRenderer()通过vue-server-renderer实例访问它有什么问题?

Why do I have to import the function directly from vue-server-renderer? What is wrong with accessing it through an instance of vue-server-renderer using vueServerRenderer.createBundleRenderer() instead of just createBundleRenderer()?

推荐答案

原因很简单,如果您要转至'vue-server-renderer',您将获得{createBundleRenderer}作为导出成员.

Reason is quite simple, If you will go to 'vue-server-renderer' you will get {createBundleRenderer} as an export member.

如果您想使用

import vueServerRenderer from 'vue-server-renderer'
const createRenderer = (serverBundle) =>
    vueServerRenderer.createBundleRenderer(serverBundle, {
        runInNewContext: false,
        template: fs.readFileSync(path.resolve(__dirname, indexFolder,'index.html'), 'utf-8')
    });

与导入有关的一些错误,您应该像导入import * as vueServerRenderer from 'vue-server-renderer'它肯定会为您工作

Some mistake is there related to import, you should import likeimport * as vueServerRenderer from 'vue-server-renderer'It should surely work for you

这篇关于为什么不能使用import通过包实例访问函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:12