先将字符串转成数组,用reverse()翻转数组,再转成字符串
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h2> {{string}}</h2>
<button @click="reverse">逆转世界</button>
</div>
<script src="../../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
string:'Hello,world',
},
methods:{
reverse(){
//先将字符串转成数组,用reverse()翻转数组,再转成字符串
this.string=this.string.split('').reverse().join('')
}
}
})
</script>
</body>
</html>