You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular项目中导入dymo.connect.framework?

在Angular中正确导入并使用Dymo Connect Framework

步骤1:确认angular.json配置并重启开发服务

你当前的angular.json脚本配置是可行的,但修改该文件后必须重启ng serve,否则配置不会生效。如果想简化配置,也可以直接写文件路径:

"scripts": [
  "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js",
  "src/assets/js/dymo/dymo.connect.framework.full.js"
]

步骤2:在组件中声明全局变量

Dymo的JS库会挂载到全局window对象上,TypeScript无法自动识别这个全局变量,需要在组件的.ts文件顶部(import语句之后)添加声明:

declare var dymo: any;

步骤3:安全调用SDK方法

确保库加载完成后再调用方法,推荐在ngAfterViewInit生命周期钩子中执行(此时DOM和外部脚本已加载):

import { Component, AfterViewInit } from '@angular/core';

declare var dymo: any;

@Component({
  selector: 'app-your-component',
  templateUrl: './your-component.component.html',
  styleUrls: ['./your-component.component.css']
})
export class YourComponent implements AfterViewInit {
  ngAfterViewInit(): void {
    // 先检查SDK是否加载成功
    if (typeof dymo !== 'undefined' && dymo.label?.framework) {
      // 示例:获取打印机列表
      const printers = dymo.label.framework.getPrinters();
      console.log('已检测到的打印机:', printers);
      
      // 此处可调用SDK其他方法,比如创建标签、执行打印等
    } else {
      console.error('Dymo Connect Framework加载失败,请检查文件路径');
    }
  }
}

可选:添加类型定义(增强TypeScript体验)

如果需要类型提示,可以手动创建类型声明文件:

  1. 在项目根目录创建src/typings/dymo.d.ts文件
  2. 添加基础类型定义(可根据SDK文档补充更多方法):
declare namespace dymo {
  namespace label {
    namespace framework {
      interface Printer {
        name: string;
        type: string;
        isConnected: boolean;
      }
      function getPrinters(): Printer[];
      // 补充你需要用到的其他SDK方法类型
    }
  }
}
  1. 在tsconfig.json中添加类型声明目录:
"include": [
  "src/**/*",
  "src/typings/**/*"
]

内容的提问来源于stack exchange,提问作者User69

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 23:23:13