如何使用 Vue.js 删除所有出现的特定字符串?
在本文中,我们将学习如何使用 VueJS 中的过滤器删除所有出现的特定单词。 Vue 是一个用于构建用户界面的渐进式框架。过滤器是 Vue 组件提供的一项功能,可让您将格式设置和转换应用于模板动态数据的任何部分。组件的过滤器属性是一个对象。单个过滤器是一个接受一个值并返回另一个值的函数。返回的值是实际打印在 Vue.js 模板中的值。
可以通过对所需字符串使用过滤器来删除特定单词的出现。我们将使用 JavaScript split()方法 在必须删除的特定目标词上拆分字符串。这将返回一个数组,该数组在该特定单词出现的点处拆分,但用于拆分字符串的单词除外。最后,我们将使用join()方法加入数组并返回结果字符串。
例子:
index.html
Original String:
{{st1}}
After Delete:
{{ st1 | delete('computer') }}
Original String:
{{st2}}
After Delete:
{{ st2 | delete('language') }}
app.js
const parent = new Vue({
el: '#parent',
data: {
st1: 'GeekforGeeks is a computer science\
portal. computer science is the study\
of algorithmic processes, computational\
machines and computation itself',
st2: 'C++ is a best language for competative
programming, Javascript is best\
for scripting language',
},
filters: {
delete: function (st, target) {
const result = st.split(target).join('')
return result;
}
}
})
应用程序.js
const parent = new Vue({
el: '#parent',
data: {
st1: 'GeekforGeeks is a computer science\
portal. computer science is the study\
of algorithmic processes, computational\
machines and computation itself',
st2: 'C++ is a best language for competative
programming, Javascript is best\
for scripting language',
},
filters: {
delete: function (st, target) {
const result = st.split(target).join('')
return result;
}
}
})
输出: