Issue
I have my RxJs code working on Node, currently it compiled to Script.js what started after TS compilation with lines
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
...
Currently I want to start my code on Browser
Of course, as first step need to overcome module support on Browser, in order to this first script is
<script>var exports = {};</script>
I don't know how is correct theoretically, but its working. Second step for me, is install and configure Require.JS, for example in this way
<script src="http://requirejs.org/docs/release/2.3.6/comments/require.js"></script>
<script type="text/javascript">
require.config({
paths: {
"rxjs": "https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.1/rxjs.umd.min.js",
"jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min"
}
});
</script>
<script type="text/javascript">
require( ["jquery"],
function ($) {
console.log($("#myButton").val())
}
);
</script>
jQuery not need for me currently, I simple check how Require.JS working with it. Yes, working fine.
But next step is link correctly RxJS and RxJs/Operator - and I how to do this correctly?
For example I replace () to [] in my Script.JS - this is requirement of ReqjireJS, it started from
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require["rxjs"];
var operators_1 = require["rxjs/operators"];
...
But Browser said "Uncaught TypeError: rxjs_1 is undefined", this mean RxJS not loaded and it's function not defined. How to overcome this (and of course not root namespace of RxJS but also RxJS/Operators).
Solution
I don't think you need to split them into rxjs/operators
and rxjs
, all are present in the single object rxjs
, please find below working example:
requirejs.config({
appDir: '.',
baseUrl: 'js',
paths: {
rxjs: ['https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.1/rxjs.umd.min'],
},
});
require(['rxjs'], function (rxjs) {
console.log('Loaded :)');
// simple
rxjs.of(true).subscribe(console.log);
// complex!
rxjs
.of(true)
.pipe(
rxjs.switchMap(() => rxjs.of('qwerty'))
)
.subscribe(console.log);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.9/require.min.js"></script>
Answered By - Naren Murali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.