Javascript vuejs如何动态更改对象中的数据
我有一个数组,如下所示:Javascript vuejs如何动态更改对象中的数据,javascript,vue.js,Javascript,Vue.js,我有一个数组,如下所示: return { address: 'London' telephone: '0044 345 6576 543422' items: [ {active: true, text: 'Company address: xyz, other info1'}, {active: true, text: 'Company telephone: xyz, other info2,'}, {active: true, text
return {
address: 'London'
telephone: '0044 345 6576 543422'
items: [
{active: true, text: 'Company address: xyz, other info1'},
{active: true, text: 'Company telephone: xyz, other info2,'},
{active: true, text: 'text3'},
{active: true, text: 'text4'},
{active: true, text: 'text5'}
...
]
}
我的意图是将模板中的数组输出为html。我的数组是某种可重用的模板,因此我需要以某种方式输出正确的数据
我尝试将computed属性与${this.address}
一起使用,但没有成功
有经验的人能告诉我怎么做吗
编辑:
{{item.text}
应显示“公司地址:伦敦,其他信息1,公司电话:0044 345 6576 543422,其他信息2,文本3,…” 请注意,这根本不是数组。这个结构更像是一个对象。在这个结构中,您应该用“,”(逗号)分隔所有标识符。所以您应该
return{
address: 'London',
telephone: '0044 345 6576 543422',
items: [
{active: true, text: 'Company address: xyz'},
{active: true, text: 'Company telephone: xyz'},
{active: true, text: 'text3'},
{active: true, text: 'text4'},
{active: true, text: 'text5'}
]
}
要访问数据,比如说我们使用的地址:
this.address;//to access address
this.items[0].text;
其中0是items对象的索引并引用全局创建的对象如果我正确理解了问题,这里的问题是您希望用数据值替换每个项目中的部分文本。为了做到这一点,你需要知道用什么替换它,以及把它放在哪里 下面是一个非常基本的例子,说明了一种可能的方法
console.clear()
新Vue({
el:“应用程序”,
数据(){
返回{
地址:伦敦,
电话:00443456576543422,
项目:[
{active:true,文本:'公司地址:[占位符],其他信息1',数据字段:'地址'},
{active:true,文本:'公司电话:[占位符],其他信息2',数据字段:'电话'},
{active:true,text:'text3'},
{active:true,text:'text4'},
{active:true,text:'text5'}
]
}
},
计算:{
activeItems(){
返回此.items.filter(item=>item.active==true)
}
},
方法:{
getInterpolatedText(文本,字段){返回文本。替换(“[placeholder]”,此[field])}
}
})
{{getInterpolatedText(item.text,item.dataField)}
这是一个对象,而不是一个阵列。您可以向我们展示您想要的输出吗?更像这样吗?这是可行的;您可能需要将数据字段更改为数组,并提出更好的占位符系统<代码>数据是一个数据属性数组,应该替换每个占位符。根据数据数组中的位置,占位符现在是{0}
或{1}
等。你的方法很好,当我将它用于简单变量时,当我在数组上使用它时,我得到了未定义:,编辑:solvedI如果你要使用这样的深度属性,最好使用像lodash这样的库。
this.address;//to access address
this.items[0].text;