使用Ember-models-table的routeName属性触发Ember警告错误
解决ember-models-table设置routeName时的警告错误
你遇到的这个警告,是因为Ember的warn函数现在要求必须传入包含id属性的options哈希作为第三个参数,而你使用的ember-models-table版本可能在内部调用warn时没满足这个要求,或者是配置细节出了问题。下面给你几个可行的解决办法:
1. 优先升级ember-models-table版本
这个警告大概率是组件的兼容性问题——旧版本的组件代码没适配Ember最新的API要求。你可以通过包管理工具更新到最新稳定版:
# 使用npm npm update ember-models-table --save # 使用yarn yarn upgrade ember-models-table
更新后再测试routeName的用法,应该就能解决这个警告。
2. 自定义跳转逻辑替代routeName属性
如果暂时无法升级,或者升级后问题依然存在,你可以放弃官方的routeName配置,手动实现行跳转逻辑,避开组件内部的警告问题:
方法一:用format函数生成链接
直接在列配置里用format函数生成可点击的链接,记得开启allowHTML:
columns: [ { propertyName: "firstName", title: "First Name", format: (value, record) => { // 替换成你的目标路由名 const routeUrl = this.router.urlFor('user.detail', record.id); return `<a href="${routeUrl}">${value}</a>`; }, allowHTML: true }, // 其他列配置... ]
方法二:使用自定义单元格组件
这种方式更符合Ember的组件化理念,也更灵活:
首先在你的组件js里添加跳转action:
actions: { goToRecordDetail(record) { this.transitionToRoute('user.detail', record.id); } }
然后配置列使用自定义单元格组件:
columns: [ { propertyName: "firstName", title: "First Name", cellComponent: 'table-name/first-name-link-cell', cellComponentOptions: { onNavigate: (record) => this.goToRecordDetail(record) } }, // 其他列配置... ]
接着创建对应的单元格模板(比如app/templates/components/table-name/first-name-link-cell.hbs):
<a {{on "click" (fn @onNavigate @record)}}>{{@record.firstName}}</a>
3. 检查routeName配置是否完整
如果坚持使用官方的routeName属性,确认你的配置是否完整:
确保同时指定了routePropertyName(默认是id,但如果你的记录主键不是id的话需要手动设置),比如:
columns: [ { propertyName: "firstName", title: "First Name", routeName: "user.detail", routePropertyName: "id" // 确保这个属性在你的记录对象上存在 } ]
内容的提问来源于stack exchange,提问作者Sow Hari
相关产品推荐
相关产品推荐

