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

Bla*_*ade 4 javascript variables vue.js

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

我的一些变量/属性:

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

现在,我想在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;
Run Code Online (Sandbox Code Playgroud)

我不想使用switch语句来检查是否需要设置新闻标题,徽章标题等,因此我想我可以将名称存储在已经在vue中定义的变量中,然后进行设置。有没有办法做到这一点?

提前致谢

Eri*_*uan 6

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

你试过了吗?self[titleElement]= selectElement.value;应该管用。