Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在VueJS中创建字符串数组?_Javascript_Arrays_Vue.js - Fatal编程技术网

Javascript 如何在VueJS中创建字符串数组?

Javascript 如何在VueJS中创建字符串数组?,javascript,arrays,vue.js,Javascript,Arrays,Vue.js,我正在使用VueJS和Vuetify创建一个可以接受文本字段中某些字符串的模式。现在我要做的是,单击,将输入字符串推入数组。比如说,如果我输入一些东西并单击“创建”,结果数组是['inputValue1'],但是如果我用逗号分隔添加另一个值,结果数组应该是['inputValue1','inputValue2'],而我得到的结果数组是['inputValue1','inputValue1''inputValue2']。因此,应该将新值推送到新索引,而不是将其与最后一个值相加 这是一个 newv

我正在使用VueJS和Vuetify创建一个可以接受文本字段中某些字符串的模式。现在我要做的是,单击,将输入字符串推入数组
。比如说,如果我输入一些东西并单击“创建”,结果数组是
['inputValue1']
,但是如果我用逗号分隔添加另一个值,结果数组应该是
['inputValue1','inputValue2']
,而我得到的结果数组是
['inputValue1','inputValue1''inputValue2']
。因此,应该将新值推送到新索引,而不是将其与最后一个值相加

这是一个

newvue({
el:“应用程序”,
数据(){
返回{
对话:错,
输入值:“”,
字符串数组:[]
};
},
方法:{
createArray(){
如果(this.inputValue!==“”){
this.stringArray.push(this.inputValue);
log(this.stringArray);
}
},
closeDialog(){
this.dialog=false;
this.inputValue=“”;
this.stringArray=[];
}
}
});

点击我
创建数组
如何创建字符串数组
接近
创造

按如下方式将值推送到数组后,应清除inputValue:

methods: {
createArray() {
  if (this.inputValue !== "") {
    this.stringArray.push(this.inputValue);
    this.inputValue = '';
    console.log(this.stringArray);
  } else {
  console.log('The inputValue is empty')
 }
},
closeDialog() {
  this.dialog = false;
  this.inputValue = "";
  this.stringArray = []
}
}
 });

将值推送到数组后,应清除inputValue,如下所示:

methods: {
createArray() {
  if (this.inputValue !== "") {
    this.stringArray.push(this.inputValue);
    this.inputValue = '';
    console.log(this.stringArray);
  } else {
  console.log('The inputValue is empty')
 }
},
closeDialog() {
  this.dialog = false;
  this.inputValue = "";
  this.stringArray = []
}
}
 });

“createArray”方法未附加到任何单击事件。除此之外,代码是正确的。:)

您的“createArray”方法未附加到任何单击事件。除此之外,代码是正确的。:)

不确定问题出在哪里,
createArray
方法将字符串正确添加到数组中。你没有把它附加到任何点击事件上。哎呀,好主意。修好了。哈哈,这解决了问题。@Eggon非常感谢你!!有时我想这就是你所需要的。如果你把它作为一个答案,我很乐意接受。不客气:)不确定你有什么问题,
createArray
方法将字符串正确地添加到数组中。你没有把它附加到任何点击事件上。哎呀,好主意。修好了。哈哈,这解决了问题。@Eggon非常感谢你!!有时我想这就是你所需要的。如果你把它作为一个答案,我很乐意接受。不客气:)是的,你也应用了这个。非常感谢。是的,这也适用。非常感谢。