加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS中的配置文件

发布时间:2020-12-17 09:05:58 所属栏目:安全 来源:网络整理
导读:什么是创建配置文件(类似于.net中的web配置),用于存储url和其他可能在应用程序部署期间变化的常量的最佳方式? 使用.constant()方法: angular.module('app').constant('MONGOLAB_CONFIG',{ baseUrl: '/databases/',dbName: 'ascrum'}); 喜欢在this example
什么是创建配置文件(类似于.net中的web配置),用于存储url和其他可能在应用程序部署期间变化的常量的最佳方式?
使用.constant()方法:
angular.module('app').constant('MONGOLAB_CONFIG',{
  baseUrl: '/databases/',dbName: 'ascrum'
});

喜欢在this example。

然后你可以只注入它需要的常量。

您可以为开发或生产定义不同的常数,然后使用Grunt等工具根据环境使用此文件或该文件。

让我们假设你有这样的文件夹结构:

|-js/
|  |-app.js
|  |-anotherfile.js
|  |-...
|
|-env/
|  |-dev.js
|  |-prod.js
|
|-index.html

dev.js和prod.js使用不同的值定义相同的.constant()服务。
然后你可以得到正确的文件连接gruntFile像这样:

grunt.registerTask('default',['concat']);

grunt.initConfig({
  env : process.env.NODE_ENV,src: {
    javascript: ['js/*.js'],config: ['env/<%= env %>.js']
  },concat: {
    javascript: {
      src:['<%= src.config %>','<%= src.javascript %>'],dest:'myapp.js'
    }
  }
});

通过运行grunt你会得到一个myapp.js文件包含良好的常量。

编辑:使用Gulp你可以这样做:

var paths = [
  'env/' + process.env.NODE_ENV + '.js','js/**/*.js',];

var stream = gulp.src(paths)
  .pipe(plugins.concat('main.js'))
  .pipe(gulp.dest('/output'));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读