简体   繁体   English

如何在角度6中导入JS文件?

[英]How Import JS File in angular 6?

how can I import a js file in angular 6. I tried to implement a navbar and it contains js functions. 如何在角度6中导入js文件。我试图实现一个导航栏,它包含js函数。 thanks!!! 谢谢!!!

I Add my js file called nav.js 我添加了名为nav.js的js文件

 "scripts": [
          "src/assets/js/nav.js"
        ]

You should include on index.html, eg: 你应该在index.html上包括,例如:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
   <title>A random project</title>
   <base href="/">

   <meta name="viewport" content="width=device-width, initial- 
    scale=1">
   <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
   <link rel="stylesheet" href="https://cartodb- 
  libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" 
/>
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.core.js"></script>

<app-root></app-root>
</body>
</html>

On this case I included cartodb.js library, then for use cartodb.js on any component, or service I use: 在这种情况下,我包括cartodb.js库,然后在任何组件或我使用的服务上使用cartodb.js:

declare var cartodb: any;

At the beginning of any file (Component, Service, etc). 在任何文件的开头(组件,服务等)。

Note change cartodb by the name of the library you will use, for example for jquery is: 注意通过您将使用的库的名称更改cartodb,例如对于jquery是:

declare var jquery:any;
declare var $ :any;

You should have something like this: 你应该有这样的东西:

import { Injectable } from '@angular/core';
// Other imports...

declare var cartodb: any;
@Injectable()
export class ARandomService {
}

Ps Search if the code you want to use doesn't exist in npm. Ps搜索您要使用的代码在npm中是否不存在。

Angular 6 has a dedicated place to declare js files within the dedicated arrays of your angular.json, under Angular 6有一个专门的位置来声明你的angular.json的专用数组中的js文件

 /projects/$(youProjectName)/architect/build/options/scripts[]

and

 /projects/$(youProjectName)/architect/test/options/scripts[]

As an example: 举个例子:

npm install fast-levenshtein --save

will install a js library under node-modules 将在node-modules下安装一个js库

The path relative to the project of the js lib file is declared like this: 相对于js lib文件的项目的路径声明如下:

"scripts": [
   "node_modules/fast-levenshtein/levenshtein.js"
]

Then declare in your component the variable(s) to use, either as described by the npm documentation or by @LuaXD 然后在组件中声明要使用的变量,如npm文档或@LuaXD所述

first, you should install your library with npm. 首先,你应该用npm安装你的库。 then the library is being added to your node_modules folder, in your project. 然后将库添加到项目中的node_modules文件夹中。

now: 现在:

if you want to import js files into the angular project, first you should check the version of angular, if you are using version 2 and 4 , you should write like this : 如果你想将js文件导入到角度项目中,首先你应该检查角度的版本,如果你使用的是版本2和4,你应该这样写:

      "scripts": [ "../node_modules/jquery/dist/jquery.min.js" ]

and if you are using version upper than 4, like 5,6 or 7 you should write like this : 如果您使用的版本高于4,如5,6或7,您应该这样写:

      "scripts": [ "./node_modules/jquery/dist/jquery.min.js" ]

in this example i import jquery into the angular project. 在这个例子中,我将jquery导入到角度项目中。 i hope, it works 我希望,它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM