admin管理员组文章数量:1355731
What is the proper workflow to include the library to angular 4.0 and use it inside a ponent?
My steps:
yarn add mathjs
Then there should be a way to injects js libraries in one of the build lifecycles so the angular4 ponent can use it. JHipster utilizes Webpack and Yarn.
Then I tried to add to Component (docs):
import { mathjs } from "./mathjs";
and
var math = require('mathjs');
Those were not working. What am I missing?
UPDATE:
It seems like mathjs uses older approach suggesting var math = require('mathjs')
. Maybe it is similar to JQuery question in some way...
UPDATE2
What is the proper workflow to include the library to angular 4.0 and use it inside a ponent?
My steps:
yarn add mathjs
Then there should be a way to injects js libraries in one of the build lifecycles so the angular4 ponent can use it. JHipster utilizes Webpack and Yarn.
Then I tried to add to Component (docs):
import { mathjs } from "./mathjs";
and
var math = require('mathjs');
Those were not working. What am I missing?
UPDATE:
It seems like mathjs uses older approach suggesting var math = require('mathjs')
. Maybe it is similar to JQuery question in some way...
UPDATE2
Share Improve this question edited Aug 7, 2017 at 15:53 J.Olufsen asked Aug 7, 2017 at 14:44 J.OlufsenJ.Olufsen 13.9k46 gold badges129 silver badges190 bronze badges3 Answers
Reset to default 9This is a great question and I'm glad you ask because I wish I had what I'm about to write the first time I encountered this little problem. This is a typescript/javascript and webpack issue before it is an angular issue. I definitely am planning a writeup on my blog soon as possible.
Your Scenario:
You run
npm install mathjs
- Now you try to use math.js from a ponent:
Find math.js dist js file (node_modules/mathjs/dist/math.js) and reference like this
import {mathjs} from "../../node_modules/mathjs/dist/math";
But you get error message saying
"set --allowJS".
You do that like this:Set --allowJS in config (tsconfig.json) { "pilerOptions": { "allowJs": true, ...
Now you get:
ERROR in ../node_modules/mathjs/dist/math.js (12209,13): Unreachable code detected.
- Looking in the math.js source, you see that it is an old school module but there is no root wrapper function (one function to bring them all and in the darkness bind them..) (more on that later).
Solution: install a typings file for the target lib (@types/mathjs)
- First, check to see if you can get @typings files for your module here https://microsoft.github.io/TypeSearch/
Grab mathjs typings file from npm (https://www.npmjs./package/@types/mathjs) and Run npm install to add the typings .d.ts files to the target lib's node_modules directory
npm install --save @types/mathjs
Add your type ref correctly
import * as mjs from "mathjs"
Use it like this:
console.log("e was: " + mjs.e);
I have the plete solution for the math.js lib on my github here https://github./res63661/importOldJSDemoWithTypings/
More: For examples look no further than your own angular project. CLI creates node_modules folder each time you run npm install after creating a new project with ng new . Dig down into here and note the d.ts files for many of the .js files.
When messing with typings or defining your own (.d.ts files) be sure to restart your server between builds as the typings don't seem to update currently on the fly
Further reading:
- http://www.typescriptlang/docs/handbook/declaration-files/consumption.html
- https://angular.io/guide/typescript-configuration#typescript-typings
- https://microsoft.github.io/TypeSearch/
Lastly:
If you are in a pinch and this is not working for you, I did have success creating a custom wrapper for a different (much smaller) module by wrapping it in a master export type
export var moduleNamer = (function(){
//module implementation
}());
then dumping the .js file local to my ponent and then referencing it as follows:
//reference like this from your ponent:
import {moduleNamer} from "./source"; //from source.js
--rich
I did this way and it worked for angular9.
First install npm package mathjs.
npm install mathjs
Then import in your ponent or directive.
import { round } from 'mathjs'
You may test with this.
console.log(round(math.pi, 3) )
Try to include the script into index.html:
<script src="./assets/math.js" type="text/javascript"></script>
Then add this into your ponent file:
declare const math;
You can then use math in your ponent:
ngOnInit(): void {
console.log(math.sqrt(-4););
}
版权声明:本文标题:javascript - How to include 3rd party library that uses older import approach to Angular4.x? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743962864a2569352.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论