1

I am building a project with Angular 6 that requires me to import styles from an external js file in order to build a custom Google Map. However, it doesn't seem to be importing into my .ts file correctly. Any help would be great!

In my component

import {MapStyles} from './mapstyle.js'

@Component({
selector: 'map',
template: '<div #tref style="width:100%; height:100%;"></div>'
})

export class Map implements AfterViewInit {
  ...
  ngAfterViewInit() {
    console.log(MapStyles)  //is 'undefined'
  }
  const mapProperties = {
      styles: MapStyles
  };
}

The file I'm trying to import (mapstyle.js):

export var MapStyles = [
  {
  "featureType": "water",
  "elementType": "geometry.fill",
  "stylers": [
    {
      "color": "#d3d3d3"
    }]
  },
 ...

I've tried things such as

import * as MapStyles from './mapstyle.js'

and

var MapStyles = [...]

but no luck. thanks!

4
  • 1
    Try saving it as a Typescript file instead of a JavaScript file Commented Apr 30, 2019 at 16:13
  • Please refer: stackoverflow.com/questions/46991237/… Commented Apr 30, 2019 at 16:14
  • @HariniP thank you but my file isn't json Commented Apr 30, 2019 at 16:21
  • 1
    @HariniP so I converted it to a .ts file, imported it like import {MapStyles} from './mapstyle', then when I used my mapProperties later in my component, I had to put <any> in front of it, but it worked! Thanks Commented Apr 30, 2019 at 16:22

2 Answers 2

1

This is how you can import and use a variable from an imported file in Angular X projects.

map-config.ts

export const mapStyle = [
    {
        'elementType': 'labels.icon',
        'stylers': [
            {
                'visibility': 'off'
            }
        ]
    },
    {
        'featureType': 'poi.medical',
        'elementType': 'labels.icon',
        'stylers': [
            {
                'visibility': 'off'
            }
        ]
    }
];

map.components.ts

import { Component, OnInit } from '@angular/core';
import { mapStyle } from './map-config';

@Component({
  selector: 'app-map', 
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})

export class MapComponent implements OnInit {

  mapStyle = mapStyle;

  constructor() { 
    // 
  }

}
Sign up to request clarification or add additional context in comments.

Comments

0

I am considering MapStyles is a variable which is from external js file.

I had a similar requirement and this is how I have solved it,

I had loaded javascript file from root component and I am trying to use the variable inside sub component as shown below.

enter image description here

Comments

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.