最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How use variable in the path while import typescript - Stack Overflow

programmeradmin0浏览0评论

Is it possible to put a varible(or a const) to the path instead of writting whole path as string literal. As it seems, angular doesn't accept anything but string literal.

import aClass = require("./simpleClass"); 
import { aComponent } from aClass.myClass.Root + 'tutorialponent';

myClass:

export class myClass{    
    public static Root = "./"
}

In this example aClass.myClass.Root + 'tutorialponent' has error which was explained

Is it possible to put a varible(or a const) to the path instead of writting whole path as string literal. As it seems, angular doesn't accept anything but string literal.

import aClass = require("./simpleClass"); 
import { aComponent } from aClass.myClass.Root + 'tutorial.ponent';

myClass:

export class myClass{    
    public static Root = "./"
}

In this example aClass.myClass.Root + 'tutorial.ponent' has error which was explained

Share Improve this question edited Apr 21, 2017 at 12:37 user663031 asked Apr 18, 2017 at 9:38 Siamak FerdosSiamak Ferdos 3,2995 gold badges34 silver badges60 bronze badges 3
  • "has error which is explained" what error? – evolutionxbox Commented Apr 18, 2017 at 9:39
  • Compiler says that path should be literal string – Siamak Ferdos Commented Apr 18, 2017 at 9:40
  • aClass.myClass.Root is undefined... Instead use aClass.Root? Although I still don't think that will work. – evolutionxbox Commented Apr 18, 2017 at 9:41
Add a ment  | 

1 Answer 1

Reset to default 7

it does support dynamic imports now..

just do this

async () => {
  const { aComponent } = await import(aClass.myClass.Root + 'tutorial.ponent');
}

for more information

http://2ality./2017/01/import-operator.html

Try this

import aClass from "./simpleClass"; 
var aComponent = require(aClass.myClass.Root + 'tutorial.ponent').aComponent;

or

import { myClass } from './simpleClass';
const { aComponent } = require(myClass.Root + 'tutorial.ponent');
发布评论

评论列表(0)

  1. 暂无评论