<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style> .active {
     color: red; } </style>
</head>
<body>
  <div id="app">
    <li v-for="(item,index) in movies" @click="change(index)" :class="getclass(index)">{
  {index}}{
  {item}}</li>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  <script> const app = new Vue({
     el: '#app', data: {
     movies: ['海王','钢铁侠','蜘蛛侠','美国队长'], isActive: true, n: -1//该值保存的是每次点击后li的index值,初始值为-1 }, methods: {
     getclass: function(index) {
     //n值与该li的index值相等才有active属性 if(this.n === index) {
     return {
    active: this.isActive} } }, change: function(index) {
     //再次点击则取消效果 if(this.n === index) {
     this.isActive = !this.isActive }else {
     this.n = index } } } }) </script>
</body>
</html>