V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wly19960911
V2EX  ›  问与答

请教 angular 组件开发编译的问题

  •  
  •   wly19960911 · 2018-06-11 14:32:04 +08:00 · 868 次点击
    这是一个创建于 2139 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近需要处理组件开发编译发布,但是在开发过程中碰到一些问题。

    因为新的 angular 不像 angular2,不允许随处 import ts 文件了,所以这里就需要编译成 js 文件。

    但是我发现在 .component.ts 中定义 @Component 的时候,发现一些导入问题。

    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.css'],
        providers: [
            MenuService,
            Description
        ]
    })
    

    其中 templateUrlstyleUrls 不能使用,否则就会出现编译之后成为 require 的方式请求,最后变成请求地址 localhost:4200/app.component.html

    有关这个问题我查看了一些 UI 框架的处理,基本都是 app.component.html 采用内联的写法,直接字符串写入 @Component 里面,然后 css 都是统一处理并且从 .angular-cli.json 导入的。

    我想问问还有什么办法处理这个问题吗?

    参考地址:how-to-build-and-publish-an-angular-module

    第 1 条附言  ·  2018-06-11 15:31:30 +08:00
    前面没有描述好,不能随便 import 非 src 里面的 ts 文件, 除非进行一些 tsconfig.json 的一些配置,这样的话,开发组件的话,必然需要编译成 js 上传到 npm 仓库才能正常被 import ( node_modules 里面的文件)。

    这个问题就是在编译成 js 的时候发生的。
    2 条回复    2018-06-11 15:30:03 +08:00
    gen900
        1
    gen900  
       2018-06-11 15:14:51 +08:00
    只有我没看懂遇到什么问题了吗?“新的 angular 不像 angular2,不允许随处 import ts 文件了” 什么意思?新的 angular 是什么?不允许随处 import,何出此言?
    wly19960911
        2
    wly19960911  
    OP
       2018-06-11 15:30:03 +08:00
    @gen900 #1 抱歉我没有描述好,从 angular4 开始 不能随便 import 非 src 里面的 ts 文件, 除非进行一些 tsconfig.json 的一些配置,这样的话,开发组件的话,必然需要编译成 js 上传到 npm 仓库才能正常被 import ( node_modules 里面的文件)。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   927 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.