问题
How would you go about passing Debug vs Release arguments to an Aurelia app?
In my case, I have Aurelia within a MVC app. I would like to configure some parameters (URLs, etc.) for Aurelia depending on my web.config values.
Is there a way I could to something like:
<script src="~/Aurelia/jspm_packages/system.js"></script>
<script src="~/Aurelia/config.js"></script>
<script>
System.import('aurelia-bootstrapper', @this.ViewBag.MyArgs);
</script>
Where this.ViewBag.MyArgs would contains arguments loaded from MVC?
I could then use those args in my Aurelia services such as
this.httpClient.configure(x => {
x.withBaseUrl(globalArgs.myBackendUrl);
});
回答1:
Register your configuration string as a module:
index.html
<script src="~/Aurelia/jspm_packages/system.js"></script>
<script src="~/Aurelia/config.js"></script>
<script>
// register a module named "my-args" that has a single export: "MyArgs" whose value is whatever the viewbag.MyArgs contains...
System.set('my-args', System.newModule({ MyArgs: '@this.ViewBag.MyArgs' }));
// standard bootstrapping logic...
System.import('aurelia-bootstrapper');
</script>
assumption here is that razor will expand { MyArgs: '@this.ViewBag.MyArgs' }
into whatever @this.ViewBag.MyArgs
returns... eg { MyArgs: 'debug=true' }
or something... you'll have to play around with that part.
Then in your javascript code you'll be able to access the args like this:
import {MyArgs} from 'my-args';
Here's a working plunker:
http://plnkr.co/edit/Scu8bN?p=preview
Here's the docs on the System.set
API:
https://github.com/systemjs/systemjs/blob/master/docs/system-api.md#systemsetmodulename-module
回答2:
I realise this has already been answered but I have an alternative suggestion that may prove useful, perhaps.
We wanted to use a different Breeze url depending on debug or release version and in the end made a ConfigController in the MVC app. This returns a json object of appropriate settings depending on the server configuration. Then in the Aurelia client we have a config module that calls the MVC endpoint using the http client and sets the client side params as required.
The ConfigController is something as simple as:
[HttpGet]
public IDictionary<string, object> Get()
{
return new Dictionary<string, object> {
{ "Application Name", "My app name" },
{ "Debug", true }
{ "APIHost", ApiSettings.APIHost },
{ "APIBaseURL", ApiSettings.APIBaseURL },
{ "LoginURL", ApiSettings.LoginURL }
};
}
and the client module contains something like:
loadConfig() {
let self = this;
let url = 'api/config';
let req = new HttpClient();
return req.get(url)
.then(result => {
return applyConfig(result);
});
}
function applyConfig(responseMessage) {
if (responseMessage.statusCode === 200) {
self.appConfig = JSON.parse(responseMessage.response);
} else {
self.appConfig = {
// Apply some defaults or something here
};
}
return self.appConfig;
}
}
One of the benefits of this is that it is easy to add settings from, for example, Azure web app environment strings. Once I'd got my head round Promises it worked well :-)
来源:https://stackoverflow.com/questions/33781268/how-to-pass-debug-vs-release-args-to-an-aurelia-app-within-asp-net-mvc