开发中经常会遇到校验 url 的问题,一般都是采用正则校验的方式。我们在这里将采用非正则的方式,通过 web api 提供的 URL API 进行校验, 详细代码如下
const uri = "https://developer.mozilla.org/en-US/docs/Web/API/URL_API";
try {
const url = new URL(uri);
} catch (e) {
console.log(e);
this.$message.warn(this.i18nTranslate(`输入的URL不合法,`));
return;
};
主要用到的API 为 URL。返回的对象包含以下内容
具体参看位置: