简体   繁体   English

Stencil JS不适用于非默认导入

[英]Stencil JS not working with non-default imports

I'm using BabylonJS in a StencilJS app, and I can only seem to import in a very specific way. 我在StencilJS应用程序中使用BabylonJS,并且似乎只能以非常特定的方式导入。

For instance I can't do: 例如,我不能:

import { Engine, Scene } from "babylonjs";

It says 'Engine' is not exported by node_modules\\babylonjs\\babylon.js But it is.. 它说'Engine' is not exported by node_modules\\babylonjs\\babylon.js但是它是..

I can do: 我可以:

import BABYLON from 'babylonjs';

and use it like 并像这样使用

private _scene: BABYLON.Scene;

I'd like for the former to work. 我想让前者工作。 Any advice? 有什么建议吗?

The first way is how most tutorials do it, and I refuse to believe SencilJS is just not capable of that. 第一种方法是大多数教程的操作方式,我拒绝相信SencilJS不能做到这一点。 I must be missing something 我肯定错过了什么

BabylonJS is provided in two versions ( ES5 and ES6 ). BabylonJS有两个版本( ES5ES6 )。 The issue you are referring to is related to ES5 version of package. 您所指的问题与软件包的ES5版本有关。

If you do smth like this in your code 如果您在代码中这样做

import * as babylon from 'babylonjs';
console.log(babylon);

and look into the console, you will see next: 并查看控制台,您将看到下一个:

{default: Module, __moduleExports: Module, babylonjs: undefined}

That's why decomposition is not working, it's not an object that can be serialized the way you expect. 这就是为什么分解不起作用的原因,它不是可以按您期望的方式序列化的对象。

In documentation they say 他们在文件中说

import { Engine, Scene } from 'babylonjs'; 从'babylonjs'导入{Engine,Scene};

NOTE: if you can't make this import method to work, go to the section on typescript and webpack below. 注意:如果无法使此导入方法起作用,请转到下面有关打字稿和Webpack的部分。

However, I failed to make it work for ES5 version. 但是,我无法使其适用于ES5版本。 The correct way, as to me would be to use ES6 version of package, which can be installed as 对我而言,正确的方法是使用ES6版本的软件包,该软件包可以安装为

npm install -S @babylonjs/core

This version allows you to use ES6 packages together with tree shaking and other useful features. 该版本允许您将ES6软件包与摇树和其他有用功能一起使用。

Your module import, in this case, would look exactly as you wish: 在这种情况下,模块导入将完全符合您的期望:

import {Engine, HemisphericLight, Mesh, Scene} from '@babylonjs/core';

Here is a small example I've done to prove my words. 这是我为证明自己的话而做的一个小例子

Please, let me know if I got you wrong and you expected to have smth different or you need some additional explanations or materials - I'll be happy to assist. 请告诉我,如果我弄错了您,并且期望与您有所不同,或者您需要其他说明或材料,我们将竭诚为您服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM