1đź‘Ť
I don’t think there’s necessarily a “best practice”, since different solutions will be optimal for different scenarios. With that being said, one way I tend to do this is by using the “revealing module pattern” with IIFEs. So it may look like this:
SomeScript.js
var SomeModule = (function () {
var someVariable = null;
return {
// someExternalVar will be passed from the caller
init: function (someExternalVar) {
someVariable = someExternalVar;
}
};
})();
Then in the Razor view:
MyRazor.cshtml
<!-- import your SomeScript.js -->
<!-- other stuff -->
<script>
SomeModule.init(@Model.someVariable);
</script>
I’m sure you could expand on this pattern to instantiate Vue using the parameter(s) passed to SomeModule.init()
.
👤Matt U
Source:stackexchange.com