gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:07:52 25 4
gpt4 key购买 nike

我想使用传入的命令行变量直接修改我的 Ember 应用程序的 index.html。所以我可以在我的 environment.js 文件中的“ENV”下设置它们,然后我可以在我的一些.js 文件通过导入 ENV。但是,在我的 index.html 文件中,即使“rootURL”变量出现,这些变量也不会出现。例如,如果我在 ENV 中设置 rootURL: "/blah"var1: "/blah2",那么如果我有类似 a href={{ rootURL}}a href = {{var1}},那么第一个链接有效而第二个链接无效。

所以我假设 {{rootURL}} 不是直接来自 ENV 变量,但我找不到它的来源。我试图对我的“app.js”中的变量做一些类似的事情,但我对这一行感到困惑:import config from './config/environment'; 这到底导入了什么?我在 environment.js 中找不到任何名为“config”的东西。是否可以在该文件 (app.js) 中设置全局变量?

最佳答案

我正在使用 ember-cli-inline-content在我的索引中使用变量,这样我就可以根据环境(生产或开发)为这样的 Assets 更改一些 url:

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

在我的 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'
}
};
}

关于javascript - 如何在 Ember 应用程序中使用全局变量,特别是在 index.html 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com