简体   繁体   English

如何在Ember应用程序中(特别是在index.html中)使用全局变量?

[英]How to use global variables in Ember app, specifically in index.html?

I would like to directly modify the index.html of my Ember app using command line variables passed in. So I can set them under 'ENV' in my environment.js file, and then I can access them in some of my .js files by importing ENV. 我想使用传入的命令行变量直接修改Ember应用程序的index.html。因此,我可以在我的environment.js文件中的“ ENV”下设置它们,然后在一些.js文件中访问它们通过导入ENV。 However, in my index.html file, these variables don't show up even though the "rootURL" variable does. 但是,在我的index.html文件中,即使“ rootURL”变量出现了,这些变量也没有出现。 For example, if I set rootURL: "/blah" and var1: "/blah2" in ENV, then if I have something like a href={{rootURL}} and a href = {{var1}} , then the first link works while the second doesn't. 例如,如果我在ENV中设置rootURL: "/blah"var1: "/blah2" ,则如果我有类似a href={{rootURL}}a href = {{var1}} ,则第一个链接有效,而第二个无效。

So I'm assuming that {{rootURL}} doesn't come directly from the ENV variable, but I can't find where it comes from. 因此,我假设{{rootURL}}不是直接来自ENV变量,但是我找不到它来自哪里。 I was trying to do something similar with variables in my 'app.js' but I'm confused about this line: import config from './config/environment'; 我试图对'app.js'中的变量做类似的事情,但是我对这行感到困惑: import config from './config/environment'; What exactly does this import? 这到底导入了什么? I can't find anything named "config" in environment.js. 我在environment.js中找不到任何名为“ config”的东西。 Is it possible to set global variables in that file (app.js)? 是否可以在该文件(app.js)中设置全局变量?

I'm using ember-cli-inline-content to use variable in my index, so I can change some urls depending the environment (production or dev) for assets like that: 我正在使用ember-cli-inline-content在索引中使用变量,因此可以根据环境(生产或开发)来更改某些网址,例如:

<link rel="stylesheet" href="{{content-for 'ASSETS_BASE_URL'}}/xxx/yyy/assets/vendor.css">

in my ember-cli-build.js file: 在我的ember-cli-build.js文件中:

if (app.env === 'development') {
  app.options.inlineContent = {
    ASSETS_BASE_URL: {
      content: 'http://lvh.me:8080'
    }
  };
}

if (app.env === 'production') {
  app.options.inlineContent = {
    ASSETS_BASE_URL: {
      content: 'https://assets.mydomain.com'
    }
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何从从 index.html 导入的 Javascript 脚本中访问全局变量和函数并在组件中使用它们? - How do you gain access to global variables and functions from Javascript scripts imported from index.html and use them in a component? 如何在不触及index.html的情况下将外部javascript标记添加到Ember JS应用程序? - How can I add an external javascript tag to an Ember JS app without touching the index.html? 如何在索引中使用 *ngIf.html angular 8 - How to use *ngIf in index.html angular 8 index.html和app.js之间的变量 - Variables between index.html and app.js 无法在 public/index.html 中获取反应应用程序的环境变量 - Unable to get env variables in public/index.html for react app 如何将 create-react-app 与提供 index.html 的后端一起使用? - How to use create-react-app with a backend which serves index.html? 如何将Heroku托管指向子目录中的angular应用和index.html? - How to point heroku hosting to angular app and index.html in subdirectory? AngularJS App:如何将.js文件包含到index.html中 - AngularJS App: How to include .js files into index.html 使用webpack访问index.html中的环境变量 - Access environment variables in index.html with webpack Ember.js:如何在index.html中正确包含编译的模板? - Ember.js: How do I include compiled templates correctly in index.html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM