惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
U
Unit 42
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
G
Google Developers Blog
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Jina AI
Jina AI
量子位
宝玉的分享
宝玉的分享
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
美团技术团队
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tailwind CSS Blog
博客园 - 司徒正美
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Privacy International News Feed
G
GRAHAM CLULEY
The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
V2EX - 技术
V2EX - 技术

博客园 - 八爻老骥

Gradle模块化项目中使用了非模块化库的编译方法 系统架构一一前端技术 系统架构一一ORM的应用 WPF下的RibbonApplicationMenu控件自定义 系统架构——多线程的应用 系统架构——前后端分离 为什么要学DirectX? D3D实战-在窗口中画一个三角形 - 八爻老骥 - 博客园 DirectX 入门1-初识DirectX Tool Kit DirectX 12 版 俄罗斯方块 WPF俄罗斯方块改进版 - 八爻老骥 - 博客园 【编程漫谈】用JAVA画多边形 【编程漫谈】单机与联机 【编程漫谈】程序的运行环境 创建DevExtreme应用程序 【编程漫谈】程序的运行流程 【编程漫谈】程序的进入与退出 【编程漫谈】Hello world! WPF实战俄罗斯方块
往Angular应用程序中添加DevExtreme
八爻老骥 · 2019-09-01 · via 博客园 - 八爻老骥

To start this tutorial, you need an Angular 5+ application created using the Angular CLI. Refer to the Angular CLI documentation for information on how to create such an application. You can also create an Angular application with DevExtreme already added to it.

一键安装

你可以用npx命令安装配置DevExtreme以及相关依赖npx是 DevExtreme CLI的一部分:

npx -p devextreme-cli devextreme add devextreme-angular

注意

npx 在 npm v5.2 及更高版本上有效。 假如你用的是早期版本,升级npm或安装全局DevExtreme CLI,然后运行命令:

npm i -g devextreme-cli
devextreme add devextreme-angular

运行完命令后,你可以直接跳过以下的文章,直接导入 DevExtreme 模块。

要是命令因某些原因不可用,以下指令可用于手动配置。

安装 DevExtreme

安装 devextreme 与 devextreme-angular 的 npm 包:

npm install devextreme@19.1 devextreme-angular@19.1 --save --save-exact

注意

因为DevExtreme没有使用Semantic版本管理,我们建议使用指定版本的DevExtreme,防止不必要的升级。我们的版本管理系统中,第一个和中间的数字表示主版本,包括行为的变更。

配置样式表

打开 angular.json ,引入 dx.common.css 及预定义的主题样式表 (dx.light.css )。

angular.json

{
  "projects": {
    "ProjectName": {
      "architect": {
        "build": {
          "options": {
            "styles": [
              "node_modules/devextreme/dist/css/dx.common.css",
              "node_modules/devextreme/dist/css/dx.light.css",
              "src/styles.css"
            ],
            ...
          },
          ...
        },
        ...
      }
    },
    ...
  },
  ...
}

对于Angular CLI 6 之前的版本,修改 angular-cli.json 成如下:

angular-cli.json

{
  "apps": [{
    "styles": [
      ...
      "../node_modules/devextreme/dist/css/dx.common.css",
      "../node_modules/devextreme/dist/css/dx.light.css",
      "styles.css"
    ],
    ...
  }],
  ...
}

注意

基于SVG的小部件不需要主题样式。你要是引用了这些样式,则小部件应用匹配的外观。

Angular CLI 6+ 中注册第三方依赖

JSZip 注册

如果你要使用 DataGrid 小部件,在tsconfig.json里注册JSZip库。这个小部件使用该库在客户端导出Excel。

tsconfig.json

{
  ...
  "compilerOptions": {
    ...
    "paths": {
      "jszip": [
        "node_modules/jszip/dist/jszip.min.js"
      ]
    }
  }
}

全局注册

你如果想语言本地化,要先安装这个包和 devextreme-cldr-data 扩展:

npm install --save-dev devextreme-cldr-data globalize

接着在tsconfig.json中注册语言包和 CLDR 脚本scripts ……

tsconfig.json
{
  ...
  "compilerOptions": {
    ...
    "paths": {
      "globalize": [
        "node_modules/globalize/dist/globalize"
      ],
      "globalize/*": [
        "node_modules/globalize/dist/globalize/*"
      ],
      "cldr": [
        "node_modules/cldrjs/dist/cldr"
      ],
      "cldr/*": [
        "node_modules/cldrjs/dist/cldr/*"
      ],
      "jszip": [
        "node_modules/jszip/dist/jszip.min.js"
      ]
    }
  }
}

……然后在src中创建 typings.d.ts 文件,Globalize, DevExtreme 本地消息, 及 devextreme-cldr-data:

typings.d.ts

declare module 'globalize' {
    const value: any;
    export default value;
}
 
declare module 'devextreme/localization/messages/*' {
    const value: any;
    export default value;
}
 
declare module 'devextreme-cldr-data/*' {
    const value: any;
    export default value;
}

使用Angular CLI 5或更早版本的项目, config.js 的配置如下:

config.js

System.config({
    // ...
    paths: {
        "npm:": "node_modules/"
    },
    map: {
        // ...
        "globalize": "npm:globalize/dist/globalize",
        "cldr": "npm:cldrjs/dist/cldr",
        "cldr-data": "npm:cldr-data",
        "json": "npm:systemjs-plugin-json/json.js",
    },
    packages: {
        app: {
            // ...
            "globalize": {
                main: "../globalize.js",
                defaultExtension: "js"
            },
            "cldr": {
                main: "../cldr.js",
                defaultExtension: "js"
            }
        }
    }
});

参考使用全球化的文章示例。

另外,可使用 Intl 这种更轻量的本地化方案。

导入DevExtreme 模块

找到将使用DevExtreme组件的 NgModule ,导入需要的 DevExtreme 模块。注意,如果应用程序中配置了撼树,则可以用 devextreme-angular导入模块。 否则,你要从特定的文件中导入他们。

app.module.ts

// ...
import { DxButtonModule } from 'devextreme-angular';
// or if tree shaking is not configured
// import { DxButtonModule } from 'devextreme-angular/ui/button';
 
@NgModule({
    imports: [
        // ...
        DxButtonModule
    ],
    // ...
})


export class AppModule { }

现在可以在应用程序中使用 DevExtreme 组件了:

app.component.htmlapp.component.ts

<dx-button
    text="Click me"
    (onClick)="helloWorld()">
</dx-button>

运行应用程序

运行应用程序的命令如下:

ng serve
Open http://localhost:4200/ to browse the application