Vue.js 匹配计算值
我试图有条件地验证以计算方式返回的值。当计算值准备就绪时,我想将其设置为数据值 然后,在与字符串内容匹配的方法中使用此数据值。检查url是否具有音频文件扩展名,如mp3、Wave。然而,即使我在字符串中明确地写下“mp3”或“wave”,我也找不到匹配项Vue.js 匹配计算值,vue.js,Vue.js,我试图有条件地验证以计算方式返回的值。当计算值准备就绪时,我想将其设置为数据值 然后,在与字符串内容匹配的方法中使用此数据值。检查url是否具有音频文件扩展名,如mp3、Wave。然而,即使我在字符串中明确地写下“mp3”或“wave”,我也找不到匹配项 <template> <div> <p>Hello There</p> </div> </div> </template> <scri
<template>
<div>
<p>Hello There</p>
</div>
</div>
</template>
<script>
export default {
name: "Musician",
data() {
return {
string: this.audioLink /*logs e.g. www.popsongs.com/groovy.mp3 */
findings: ''
}
},
computed: {
hasAudioFile() {
return this.audioLink;
}
},
mounted() {
this.runMatch();
},
methods : {
runMatch: function() {
let urlPath = this.audioLink;
this.findings = urlPath.match(/mp3|wave/gi);
if(this.findings === 'mp3') return console.log('This is a MP3 file');
else if (this.findings === 'wave') return console.log('This is a Wave file');
else return console.log('No sound files found');
/* The example url has a .mp3, but I still get the final log i.e. No sounds files found*/
}
}
};
你好
导出默认值{
姓名:“音乐家”,
数据(){
返回{
字符串:this.audioLink/*日志,例如www.popsongs.com/groovy.mp3*/
调查结果:“”
}
},
计算:{
hasAudioFile(){
返回此.audioLink;
}
},
安装的(){
这个.runMatch();
},
方法:{
runMatch:function(){
让urlPath=this.audioLink;
this.findings=urlPath.match(/mp3 | wave/gi);
if(this.findings===“mp3”)返回console.log(“这是一个mp3文件”);
else if(this.findings===“wave”)返回console.log(“这是一个wave文件”);
else返回console.log(“未找到声音文件”);
/*示例url有一个.mp3,但我仍然得到最终日志,即没有找到声音文件*/
}
}
};
Match返回一个数组,而不是一个字符串。通过将结果记录到控制台,您知道,根据值的不同,它当然不会等于“mp3”作为字符串,您可以这样做:
if(this.findings[0] === 'mp3') return console.log('This is a MP3 file');
else if (this.findings[0] === 'wave') return console.log('This is a Wave file');
else return console.log('No sound files found'); //you also have a missing quote in this line
您的代码中有很多小错误,例如缺少引号或逗号,但尽管我已经回答了如何解决您发布的错误,但您似乎没有显示该文件的定义位置。audioLink。另外,使用名为“string”的数据属性可能不是一个好主意。不知道JavaScript和/或Vue是否接受它(从未尝试过),但可能会让人困惑。谢谢你,Tim的建议。是的。audioLink是代码中其他地方的返回值,我没有包括。感谢您的时间和帮助。还有一层代码没有记录正确的值。现在我相信我可以使用some()或includes():)@Drax不客气。是的,你当然可以,如果你需要帮助,我们就在这里