Javascript 导入typescript时如何在路径中使用变量
是否可以将varibleor const放入路径,而不是将整个路径作为字符串文字写入。看起来,angular只接受字符串文字Javascript 导入typescript时如何在路径中使用变量,javascript,angular,typescript,Javascript,Angular,Typescript,是否可以将varibleor const放入路径,而不是将整个路径作为字符串文字写入。看起来,angular只接受字符串文字 import aClass = require("./simpleClass"); import { aComponent } from aClass.myClass.Root + 'tutorial.component'; myClass: export class myClass{ public static Root = "./" } 在本例中
import aClass = require("./simpleClass");
import { aComponent } from aClass.myClass.Root + 'tutorial.component';
myClass:
export class myClass{
public static Root = "./"
}
在本例中,aClass.myClass.Root+“tutorial.component”出现了错误,对此进行了解释
是否可以在路径上放置变量或常量
不可能,因为ES6导入是静态的
另见:
,在Axel Rauschmayer的书中。
是否可以在路径上放置变量或常量
不可能,因为ES6导入是静态的
另见:
,在Axel Rauschmayer的书中。
它现在支持动态导入 就这么做吧
async () => {
const { aComponent } = await import(aClass.myClass.Root + 'tutorial.component');
}
更多信息
试试这个
import aClass from "./simpleClass";
var aComponent = require(aClass.myClass.Root + 'tutorial.component').aComponent;
或
它现在支持动态导入 就这么做吧
async () => {
const { aComponent } = await import(aClass.myClass.Root + 'tutorial.component');
}
更多信息
试试这个
import aClass from "./simpleClass";
var aComponent = require(aClass.myClass.Root + 'tutorial.component').aComponent;
或
有错误,解释了什么错误?编译器说路径应该是文本stringaClass.myClass.Root未定义。。。而是使用aClass.Root?虽然我仍然不认为这会起作用。有错误,这解释了什么错误?编译器说路径应该是文本stringaClass.myClass.Root未定义。。。而是使用aClass.Root?虽然我仍然不认为这会起作用。它起作用了,谢谢@siamak ferdos这应该是一个公认的答案,它在角度上非常好!从“环境”导入{env};导出函数getsomejson{return'assets/path/'+env.some_var+'/file.json'},然后在component.ts中只需:从'path'导入{getsomejson}。。。style=getsomejson;,在html中,您只需要:[style]=style | asyncIt就可以了,谢谢@siamak ferdos这应该是一个公认的答案,它在角度上非常好!从“环境”导入{env};导出函数getsomejson{return'assets/path/'+env.some_var+'/file.json'},然后在component.ts中只需:从'path'导入{getsomejson}。。。style=getsomejson;,在html中,您只需要:[style]=style | async