css - Angular 4 ng构建产品样式SyntaxError

标签 css angular typescript angular-cli dev-to-production

Angular 4.3.3, Angular cli,MVC

当我在我的开发环境中使用 ng build 编译我的代码时,一切正常,我可以看到样式。

但是当我为生产环境执行 ng build -prod 时,编译成功,我确实获得了所有 js 包文件。

但是在浏览器(Chrome)中我得到一个错误:

未捕获的语法错误:无效或意外的标记

在文件 styles.14098r3...bundle.css

在我的 tsconfig.json 中,目标:“es5”。

为什么在我的开发环境中一切正常,但在我的产品中风格却不起作用?

请帮我解决这个问题。

编辑:

我在开发中,样式的 html 标签如下所示:

<script src="/dist/styles.bundle.js"></script>

但在 prod html 中的样式如下:

<script src="/dist/styles.149025e8452.bundle.css">

如果我将 prod 样式标签更改为链接而不是脚本,样式工作正常。

---我从 mvc 包生成脚本---

最佳答案

我遇到了同样的问题。这是我最终做的:

在你的包配置中:

bundles.Add(new ScriptBundle("~/bundles/angular-components")
                .Include("~/Client/app/dist/inline.bundle.js")
                .Include("~/Client/app/dist/polyfills.bundle.js")
                .Include("~/Client/app/dist/styles.bundle.js")
                .Include("~/Client/app/dist/vendor.bundle.js")
                .Include("~/Client/app/dist/main.bundle.js")
                .IncludeDirectory("~/Client/app/dist/", "*.js.map", false));


bundles.Add(new StyleBundle("~/bundles/angular-components-style").Include(
                "~/Client/app/dist/styles.bundle.css")
            );

在您的布局中,您同时导入样式和脚本包:

@Styles.Render("~/bundles/angular-components-style")
@Scripts.Render("~/bundles/angular-components")

当您构建 Angular 应用程序时,请使用无哈希选项:

ng build --prod --watch -oh none

这样,无论您使用或不使用 --prod 选项进行构建,您的输出文件都不会包含任何哈希值。在开发环境中,它会将 styles.bundle.js 呈现为脚本,而在生产环境中构建时,它会使用 styles.bundle.css> 将其呈现为链接/p>

关于css - Angular 4 ng构建产品样式SyntaxError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45857469/

相关文章:

HTML/CSS - 无法选择的行号

css - 如何从 html 中删除主机组件标签

typescript - 除非在每个文件中引用,否则无法在 TS 文件中找到名称 '$'

javascript - RxJS - 无限滚动 - 在上一个未到达时发送请求

css - 获取任何网站的颜色代码和背景图像文件

css - 伪图像无效后

javascript - 如何将 polymer 纸标签对齐到右侧

angular - 垫 slider : not updating immediately on drag

Angular 变化检测不适用于 HttpClient.Subscribe 中的 ChangeDetectionStrategy.OnPush

javascript - 用于 TypeScript : Class or Interface? 中的纯数据对象的内容