Vue.js访问具有不确定名称的变量

时间:2017-03-24 14:33:49

标签: javascript variables vue.js

我想知道是否可以从vue的数据集合中访问变量,但只能通过另一个变量指定变量名称。这就是我的意思:

我的一些变量/属性:

var siteAdminVue = new Vue({
  el: "#siteAdmin",
  data: {
    tagtitle: "",
    tagparent: "",
    tagdesc: "",
    badgestitle: "",                    
    badgesdesc: "",               
    badgesprivilege: 0,                  
    newstitle: "",                    
    newsnotify: false,                  
    newscontent: "", 
}             

现在我想在vue的一个方法中设置一些这些属性:

  var self = this;
  var type = currentSection.toString().substr(4);
  var selectElement = document.getElementById(currentSection + "select");

  // name of vue property, for example newstitle
  var titleElement = type + "title";    

  self.[titleElement]= selectElement.value;

我不想使用switch语句检查是否需要设置newstitle,badgestitle等等,所以我想我可以将名称存储在已经在vue中定义的变量中然后设置它。有没有办法做到这一点?

提前致谢

1 个答案:

答案 0 :(得分:2)

当然,您可以将Vue的数据视为js对象,这意味着您可以通过点/括号表示法获取和设置属性。

你试过吗? j***@e***.com 应该有用。