typescript中能通过import引入html文件嘛?

typescript中能通过import引入html文件嘛

阅读 9.6k
5 个回答

不能用import语法,要使用require,然后用webpack打包下

不能。。。。。。

不能,要用webpack

关键在你的加载器支不支持 require html。
和用不用 typescript 没关系。

可以直接使用require, 因为在node或webpack(@types/webpack-env)环境下, require 被声明为类似这样一个函数:

declare var require: NodeRequire;
interface NodeRequire {
  <T>(path: string): <T>;
}

所以为了更好地区分开静态资源和Javascript模块, 建议始终使用require来导入静态资源, 使用import来导入Javascript模块.
如果非得要使用import来导入, 比如你想要更好地类型化静态资源模块, 可以使用通配符模块声明(Wildcard module declarations), 如:

// global.d.ts
declare module "*.html" {
  const content: string
  export = content
}
// svg-sprite-loader
declare module "*.svg" {
  const value: {
    viewBox: string
    id: string
    content: string
  }
  export = value
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进