vue.js - 从 Vue 模板中读取所有元素绑定
问题描述
我正在尝试预处理 Vue 2 模板并获取所有元素绑定的列表。因此,如果我有这样的文件:
<html>
<body>
<div id="app">
<p>Here's a message: {{message1}}</p>
<p>Here's an input: <input type="text" v-model="message2"></p>
</div>
<script type="application/javascript" src="vue.js"></script>
<script type="application/javascript">
new Vue({
el: "#app",
data: {
message1: "foo",
message2: "bar"
}
});
</script>
</body>
</html>
然后某个地方(在Mount 之前?)我可以查询Vue,它会告诉我绑定是['message1','message2']。那可能吗?
解决方案
我最终通过获取渲染函数的文本(通过调用 vm.$options.render.toString() )然后从中解析绑定来解决这个问题。
例如,简单列表视图的渲染函数如下所示:
function() {
var _vm = this
var _h = _vm.$createElement
var _c = _vm._self._c || _h
return _c(
"table",
{ attrs: { border: "1", cellpadding: "5", cellspacing: "0" } },
[
_vm._m(0),
_vm._l(_vm.rows, function(row) {
return _c("tr", [
_c(
"td",
[
_c("router-link", { attrs: { to: "/detail/" + row.ID } }, [
_vm._v(_vm._s(_vm._f("truncate")(row.TITLE, 100)))
])
],
1
),
_c("td", [_vm._v(_vm._s(_vm._f("truncate")(row.DESCRIPTION, 200)))]),
_c("td", [_vm._v(_vm._s(row.TYPE))])
])
})
],
2
)
}
看起来绑定总是包含在 _s() 元素中,并且在使用过滤器时可以选择vm.f () 指令。
推荐阅读
- html - 文本在css中悬停时仍然隐藏?
- r - 如何编码以 R 语言拆分名称
- java - 使用组合键 Hibernate 自定义加入
- ubuntu - 具有两个接口和两个子网的 ubuntu 的 netplan 配置,其中一个用于 Web 流量
- python - xlwings 如何分离 py 和 excel 文件?
- c# - 柱和源系列必须来自相同的时间帧和符号参数名称:parameterValues
- json - 如何使用 Nix 读取 JSON 文件?
- groovy - 詹金斯管道:执行远程脚本:错误:第1行:html:没有这样的文件或目录
- python - PyQt5 在更改样式表时使用默认图像图标
- pycharm - 在 conda 中创建环境时如何修复错误