The header calls the click method getticket()
<template>
<div>
<button @click="getticket()">获取ticket</button>
<button @click="noticket()">删除ticket</button>
<p>{
{ticket}}</p>
</div>
</template>
Define a url at will, and define the method getticket() in methods
//这是获取
<script>
export default {
data() {
return {
url: "https://example.com/index.html?ticket=9b68dd98306327bf#&id=15",
ticket: ""
}
},
methods: {
getticket() {
//获取url(字符串)
var str = this.url;
//利用indexOf("参数")的返回值来判断
var result = str.indexOf("ticket");
//判断indexOf的返回值,如果不为-1,则为真
if (result > 0) {
//取出ticket里的值
var beginIndex = str.indexOf("=");
var endIndex = str.indexOf("#");
this.ticket=str.substring(beginIndex + 1, endIndex)
} else {
this.ticket="not found"
}
}
}
}
</script>
Define the method noticeet() in methods
getticket() {
//获取url(字符串)
var str = this.url;
var result = str.indexOf("ticket"); //利用indexOf("参数")的返回值来判断
if (result > 0) {
var beginIndex = str.indexOf("?");
var endIndex = str.indexOf("#");
this.ticket = str.substring(0, beginIndex) + str.substring(endIndex)
} else {
this.ticket = "not found"
}
}
Because the url is generally in the form of a string, I tried to extract it in this way.