首页 > web前端 > js教程 > 正文

如何在 WordPress 网站中使用 Importmap

DDD
发布: 2024-09-13 18:24:01
转载
427人浏览过

如何在 wordpress 网站中使用 importmap

我一直在尝试开发一个基本的 wordpress 经典主题,无需构建步骤,我可以将其用作入门主题,以便将来开发客户端站点。在撰写本文时,我没有做任何自由职业,因为我正在为一家网络机构工作,并且我们正在构建的网站都涉及构建步骤。所以我想写一个关于如何在 wordpress 主题中使用 importmap 的简短教程。

career tracker 是我现有的一个副项目,它已经使用了 importmap,无需构建步骤,但它是一个纯 javascript 应用程序。

让我们看看如何在 wordpress 世界中做到这一点。

入队模块脚本

在我的主题functions.php中,我使用wordpress中的wp_enqueue_script_module函数将我的javascript文件app.js作为模块脚本排入队列。

wp_enqueue_script_module( 'frontend-scripts', gearup_theme_url . '/static/js/app.js', [], gearup_theme_version, true );
登录后复制

这将输出到前端的以下脚本标签。

<script type="module" src="http://test.local/wp-content/themes/gearup/static/js/app.js?ver=0.1.0" id="frontend-scripts-js-module"></script>
登录后复制

我的 javascript 文件被放置在主题文件夹的 static 文件夹中。

static
├── css
│   ├── app.css
│   ├── global.css
│   ├── reset.css
│   ├── utils.css
│   └── variables.css
└── js
    ├── admin.js
    ├── app.js
    ├── components
    │   └── menu.js
    └── utils
        └── index.js
登录后复制

正如您在此文件结构中所看到的,我需要将 utils 文件夹中的 index.js 和 components 文件夹中的 menu.js 导入到我的 app.js 中。在添加 importmap 之前,让我们看看当我在 app.js 中导入这两个文件时它的样子。

// utils
import { ondocumentready } from './utils/index.js';
// components
import menu from './components/menu.js';
登录后复制

但是我想要的是像这样导入文件。

// utils
import { ondocumentready } from 'utils/index.js';
// components
import menu from 'components/menu.js';
登录后复制
登录后复制

一旦我将导入更改为这种格式,浏览器将在控制台中抛出此错误。

uncaught typeerror: failed to resolve module specifier "utils/index.js". relative references must start with either "/", "./", or "../".
登录后复制

importmap 来救援

将其添加到模板 html head 标签中。您可能需要在 php 中渲染此部分,以便获得静态文件夹的动态 url。

<script type="importmap">
    {
      "imports": {
        "utils/": "/wp-content/themes/gearup/static/js/utils/",
        "components/": "/wp-content/themes/gearup/static/js/components/"
      }
    }
</script>
登录后复制

在我的 app.js 中使用它

现在有了 importmap 设置,即使这不是 node 环境,我们仍然可以在我们熟悉的结构下导入文件。请记住,文件需要以 .js 结尾。

// utils
import { ondocumentready } from 'utils/index.js';
// components
import menu from 'components/menu.js';
登录后复制
登录后复制

如果我将 .js 从 utils/index.js 删除到 utils/index,那么浏览器将在控制台中记录此错误。

get http://test.local/wp-content/themes/gearup/static/js/utils/index net::err_aborted 404 (not found)
登录后复制

将 cdn 中的文件添加到我们的导入映射中

<script type="importmap">
    {
      "imports": {
        "utils/": "/wp-content/themes/gearup/static/js/utils/",
        "components/": "/wp-content/themes/gearup/static/js/components/",
        "ccw/": "https://unpkg.com/cucumber-web-components@0.5.2/dist/"
      }
    }
</script>
登录后复制

我获取了指向我的 web 组件集合的 cdn 链接,并将其添加到我的导入映射中。添加后,现在我们可以像这样将 web 组件导入到 app.js 中。这不是很漂亮吗?

import "ccw/side-nav/index.js";
import "ccw/side-nav-item/index.js";
import "ccw/icon/index.js";
import "ccw/form-layout/index.js";
import "ccw/text-field/index.js";
import "ccw/email-field/index.js";
import "ccw/date-picker/index.js";
import "ccw/option/index.js";
import "ccw/select/index.js";
登录后复制

对于 web 组件,显然我没有在我的 wordpress 主题中使用它,但你可以检查我在开头提到的副项目 career tracker,看看它们是如何工作的。

以上就是如何在 WordPress 网站中使用 Importmap的详细内容,更多请关注php中文网其它相关文章!

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号