可以使用 JavaScript 中的 String.prototype.includes()
方法来判断字符串中是否包含某个子字符串。
let str = '<li class="line">';
if (string.includes(str)) {
// 字符串中包含 str
} else {
// 字符串中不包含 str
}
如果要在 Vue 组件中判断,可以将这段代码放在 Vue 组件的方法中,然后在组件模板中调用这个方法。
例如:
<template>
<div>
<button @click="checkString">检查字符串</button>
</div>
</template>
<script>
export default {
methods: {
checkString() {
let str = '<li class="line">';
if (this.string.includes(str)) {
console.log('字符串中包含 str');
} else {
console.log('字符串中不包含 str');
}
}
}
}
</script>