javascript - 使用 ${} 访问 vue-model 属性
问题描述
我有一个小片段在我使用时可以正常工作:src="labels[index]"
,但是我的代码正在增长,我需要使用${app.labels[index]}
. 我可以这样做${app.labels}
,但是一旦我添加了该[index]
部分,它就会中断。有人可以告诉我如何在括号内添加它吗?
new Vue({
el: "#app",
data: {
images: [],
labels: [],
form: {
dir: "",
fileItems: [] // An array containing objects of type: {id: number, file: File, dataUrl: [base64 encoded file string], caption: string'}
},
},
methods: {
toggle: function(todo){
todo.done = !todo.done
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>Todos:</h2>
<div class="image-wrap"><img style="max-height: 430px;" :src="${app.labels[index]}" /></div>
</div>
解决方案
在 Vue 中,当您:
在道具中使用时,您基本上是在告诉 Vue
请将此表达式解释为 javascript
一个例子:
<super-aweosme-component :a="1 + 1" />
将导致 propa
存在2
(因为它被解释为 javasript)。
当你不使用:
组件道具时,你是在告诉 Vue
请将此解释为纯文本
一个例子
<super-aweosme-component a="1 + 1" />
将导致道具a
字面意思1 + 1
(字符串)。所以你可以更改:src="${images.labels[index]}"
为 just :src="images.labels[index]"
(当然,应用上面提到的更改)
加法
你有app.labels[index]
. app
在模板上下文中不存在。你在那个上下文中images
,labels
并且form
(当然,在那个上下文中有属性,但对于这部分并不重要)
推荐阅读
- java - 从方法返回多个系列
- c# - 当我尝试打开 PDF 时,Xamarin C#“我们无权打开此文件”
- azure-blob-storage - ADLS Gen2 上的 Polybase - CSV 文件作为源失败并出现“HdfsBridge::recordReaderFillBuffer”错误
- javascript - 异步调用后如何自动对焦选择输入?
- angular - 单个水疗中心不会同时显示两个应用程序
- python - 无法将 NumPy 数组转换为张量(不支持的对象类型浮点数)
- r - 如果满足三个条件,R子集3d数组并修改特定值
- python - 如果尚不存在,Django 多对一创建父(“一个”)条目
- ruby-on-rails - 将 puma / systemd 与套接字激活一起使用有什么好处?
- c# - 开发与 .NET Core 1 和 .NET Core 3 相同的 .NET Core 源代码