how to use vanilla js iterate the Symbol Object All In One
how to use vanilla js iterate the Symbol Object All In One
bug ❌
Uncaught TypeError: UIComponents is not iterable
import * as UIComponents from './index'
console.log(`UIComponents =`, UIComponents);
// UIComponents = Module {Symbol(Symbol.toStringTag): 'Module'}
export const UIComponentsInstall = {
// 钩子函数
install: (app: App) => {
// Uncaught TypeError: UIComponents is not iterable ❌
for (const component of UIComponents) {
// 把组件挂载到 Vue 上
app.component(component.name, component);
}
}
}
solutions ✅
Vue 组件注册
// type ???
import type {App} from "vue"
// 自动注册所有组件
import * as UIComponents from './index'
console.log(`UIComponents =`, typeof UIComponents, UIComponents);
// UIComponents = Module {Symbol(Symbol.toStringTag): 'Module'}
export const UIComponentsInstall = {
// 钩子函数
install: (app: App) => {
// Uncaught TypeError: UIComponents is not iterable ❌
for (const [name, component] of Object.entries(UIComponents)) {
console.log(`name =`, name);
console.log(`component.name =`, component.name);
// 把组件挂载到 Vue 上
app.component(component.name, component);
}
}
}
- vanilla js
Object.entries
copy(temp1)
Object.entries(temp1)
for (let [key, value] of Object.entries(temp1)) {
console.log('key, value =', key, value)
}
- lodash-es
$ yarn add -D lodash-es @types/lodash-es
loadsh forEach
import * as UIComponents from './index'
export const UIComponentsInstall = {
// 钩子函数
install: (app: App) => {
forEach(UIComponents, (component) => {
// 把组件挂载到 Vue 上
app.component(component.name, component);
})
}
}
???
let str = "Hello";
// does the same as
// for (let char of str) alert(char);
let iterator = str[Symbol.iterator]();
while (true) {
let result = iterator.next();
if (result.done) break;
alert(result.value); // outputs characters one by one
}
https://javascript.info/iterable
https://www.youtube.com/watch?v=CM_oBrnB4Vk&ab_channel=codebubb
https://www.youtube.com/watch?v=2oU-DfdWM0c&ab_channel=dcode
refs
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16648418.html
未经授权禁止转载,违者必究!
标签:
bug
, iterator protocol
, iterators
, lodash
, Symbol
, Object.entries
, 组件注册
, Vue
, JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-09-02 vue 单文件组件, 分离 UI 和 Template 和 js All In One
2021-09-02 线上环境 token 更新后自动推送到测试环境 All In One
2021-09-02 CSS inline-block & font-size bug All In One
2021-09-02 Snowpack All In One
2020-09-02 node.js 中间件
2020-09-02 Web 前端页面性能监控指标 All In One
2020-09-02 Redis 大 key 问题 & 问题分析 & 解决方案 All In One