美文网首页web前端
angular-全局变量

angular-全局变量

作者: 姜治宇 | 来源:发表于2020-11-25 19:49 被阅读0次

某些库的命名空间可能需要全局注入,比如jquery的$,openlayer的ol等等,我们使用declare关键字可以搞定这件事。
先在index.html模板中引入库:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>NgRouter</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script src="assets/libs/ol.js"></script>
  <link rel="stylesheet" href="assets/libs/ol.css">
</head>
<body>
  <app-root></app-root>
</body>
</html>

在需要使用的地方声明:

import { Component, OnInit } from '@angular/core';
declare var ol:any; // 声明全局变量
@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss']
})
export class ExampleComponent implements OnInit {
  
  constructor() { }
  ngOnInit(): void {
    console.log(ol);
  }

}

相关文章

  • angular-全局变量

    某些库的命名空间可能需要全局注入,比如jquery的$,openlayer的ol等等,我们使用declare关键字...

  • angular.js(2) 数据的渲染

    angular-(2) 把数据添加到组件中 eg:ng generate component user-item在...

  • angular-随笔

    And God said,Let there be light:and there was light. 前端知名...

  • Angular-路由

    路由与导航 在用户使用应用程序时,Angular 的路由器能让用户从一个视图导航到另一个视图。 概览 Angula...

  • angular-管道

    管道其实是一些简单的函数,可以在模板表达式中用来接受输入值,并返回一个转换后的值。管道分为内置管道和自定义管道两种...

  • angular-指令

    指令分为两种大的类型:带模板的指令和不带模板的指令。带模板的指令就是component组件,不带模板的指令又有两种...

  • angular-路由

    使用路由需遵循如下步骤: 1、导入路由模块 如果在运行ng new xxx命令时,没有选择路由选项,我们可以手动安...

  • angular-服务

    按照传统设计理念,在组件(component)中不应该直接获取或保存数据, 组件应该聚焦于展示数据,而把数据访问的...

  • Angular-个人整理

    单向从数据源到视图 单向从视图到数据源 双向 DOM property 的值可以改变;HTML attribute...

  • angular-装饰器

    装饰器在angular中大量使用,有必要单独拎出来说一下。装饰器顾名思义,就是装饰用的,装饰什么呢?主要是类、属性...

网友评论

    本文标题:angular-全局变量

    本文链接:https://www.haomeiwen.com/subject/utqwiktx.html