翻转字符串
用Vue实现一个将字符串翻转的功能
效果
简单字符串反转
代码
使用JS中字符串的Split,Reverse和Join方法做字符串的反转,使用v-on:click绑定Vue事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>lesson 2</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
Vue.createApp({
data() {
return {
content: 'hello world'
}
},
methods: {
handleBtnClick() {
this.content = this.content.split('').reverse().join('');
}
},
template: `
<div>
{{content}}
<button v-on:click="handleBtnClick">反转</button>
</div>
`
}).mount('#root');
</script>
</html>