3

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.component';

myClass:

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

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

3
  • "has error which is explained" what error? Commented Apr 18, 2017 at 9:39
  • Compiler says that path should be literal string 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. Commented Apr 18, 2017 at 9:41

1 Answer 1

7

it does support dynamic imports now..

just do this

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

for more information

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

Try this

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

or

import { myClass } from './simpleClass';
const { aComponent } = require(myClass.Root + 'tutorial.component');
Sign up to request clarification or add additional context in comments.

2 Comments

It works, thanks! @siamak-ferdos that should be an accepted answer
Works great in Angular! import { env } from 'environments'; export function getsomejson(){ return 'assets/path/' + env.some_var + '/file.json' } and then in the component.ts you just: import {getsomejson} from 'path'; ... style = getsomejson();, and in the html you just need: [style]="style | async"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.