2016-05-23 90 views
0

我试图让我的角度谷歌地图工作在我的ES6语法。 在ES5它看起来像这样:Angular配置ES6语法

.config(function(uiGmapGoogleMapApiProvider) { 
    uiGmapGoogleMapApiProvider.configure({ 
    // key: 'your api key', 
    v: '3.20', 
    libraries: 'weather,geometry,visualization' 
}); 
}) 

在ES6我这样做,但我拿到“配置”是不是一个函数。

export default function uiGmapGoogleMapApiProvider() { 
uiGmapGoogleMapApiProvider.configure({ 
    // key: 'your api key', 
    v: '3.20', 
    libraries: 'weather,geometry,visualization' 
}); 
} 

我该如何正确写入es6?谢谢!

回答

1

您需要注入您的依赖关系。

angular.module('yourApp') 
    .config(mapConfig); 

mapConfig.$inject = ['uiGmapGoogleMapApiProvider']; 

function mapConfig(uiGmapGoogleMapApiProvider) { 
    uiGmapGoogleMapApiProvider.configure({ 
     // key: 'your api key', 
     v: '3.20', 
     libraries: 'weather,geometry,visualization' 
    }); 
} 

要'使用'es6我认为你的意思是类。如果你想使用一个类,使用构造函数。

mapConfig.$inject = ['uiGmapGoogleMapApiProvider']; 

export default class mapConfig { 
    constructor(uiGmapGoogleMapApiProvider) { 
     uiGmapGoogleMapApiProvider.configure({ 
      // key: 'your api key', 
      v: '3.20', 
      libraries: 'weather,geometry,visualization' 
     }); 
    } 
} 
+0

谢谢。但是如果我已经有一个配置呢?这种情况下的路由提供者。 – user2952238

+0

你可以在多个地方调用.config。使用基于组件的设计,每个组件都应配置自己 – d3l33t

+0

嗯,没有工作。它说$注入是未定义的。 – user2952238