使用 Meteor 为每个模板动态加载 JS/CSS [英] Dynamically load JS/CSS per template with Meteor

查看:37
本文介绍了使用 Meteor 为每个模板动态加载 JS/CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解几个问题(即 this) 已经发布了这种性质,但是没有找到可靠的解决方案.从表面上看,Meteor 目前缺乏动态加载/呈现不同 UI JavaScript 的能力(即 uilang) 或每个模板的 CSS 文件.在我的应用程序中,我的模板需要特定的 JS 库和 CSS,而不是其他模板.

I understand a couple questions (i.e. this) of this nature have already been posted, however no solid solution has been found. From what it seems, Meteor currently lacks of the ability to dynamically load/render different UI JavaScript (i.e. uilang) or CSS files per template. In my application, I have templates that require specific JS libraries and CSS as oppose to other templates.

例如:

user.html requires -> uilang.js, user_ui_code.js, userstyle.css
admin.html requires -> uilang.js, admin_ui_code.js, admin_style.css

我需要根据请求的路由(即 example.com/userexample.com/admin)渲染/加载每个 js/css 文件,对于不同的背景颜色、过渡等.Meteor 文档说明:

I would need each js/css file to render/load depending on which route gets requested (i.e. example.com/user or example.com/admin), for things like different background-colors, transitions, etc. Meteor documentation states:

/public 中的文件按原样提供给客户端.使用它来存储图像等资产.例如,如果您有一个位于/public/background.png 的图像,您可以使用 background-image: url(/background.png) 将它包含在您的 HTML 或 CSS 中.请注意,/public 不是图像 URL 的一部分.

Files in /public are served to the client as-is. Use this to store assets such as images. For example, if you have an image located at /public/background.png, you can include it in your HTML with or in your CSS with background-image: url(/background.png). Note that /public is not part of the image URL.

所以你会认为把你所有的 js/css/images 资产放在公共中并在 head 中静态调用所需的文件可以解决这个问题.取而代之的是,JS(以及 CSS)文件似乎被连接在一起,并且无论您在哪条路线上,它们都同时运行,因此重叠了代码.我不确定我是否遗漏了一些明显的问题,或者这是 Meteor 中的实际问题?

So you would think that putting all your js/css/images assets in public and calling the needed files statically in head would solve the problem. Instead, it seems that the JS (CSS as well) files get concatenated and it all runs at the same time regardless of which route you are on, thus overlapping code. I'm not sure if I am missing something blatantly obvious or is this an actual issue in Meteor?

感谢您的帮助.

推荐答案

Meteor 尚不支持延迟/按需加载资源. 该功能在路线图上为 "incremental加载".

Meteor doesn't yet support lazy/on demand loading of resources. The feature is on the roadmap as "incremental loading".

与此同时,请查看 numtel:publicsourcesnumtel:privatesources,它允许您为延迟加载资源创建包,无论是否通过身份验证.

In the meantime, have a look at numtel:publicsources and numtel:privatesources, which let you create bundles for lazy loading resources, with or without authentication.

这篇关于使用 Meteor 为每个模板动态加载 JS/CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆