es6 class写法 报错为什么?

作者站长头像
站长
· 阅读数 22

第一种写法为什么会报错

写法一 报错

export class Swip1{
    methods:{
        swipeHandler: function(params) { <- 这个地方提示有错
          if(-1 < temp && temp< this.magjorCategoryList.majorCategory.length){
            this.setSelect = temp;
            console.log(this.magjorCategoryList.majorCategory[temp])
            this.changeSelect(temp,this.magjorCategoryList.majorCategory[temp])
            this.$bus.$emit("activeTabToCenter", document.getElementById('label' + temp));
          }
        }
    }
}

写法二 没问题

export let Swip = {
  methods: {
    swipeHandler: function (temp) {
      if(-1 < temp && temp< this.magjorCategoryList.majorCategory.length){
        this.setSelect = temp;
        console.log(this.magjorCategoryList.majorCategory[temp])
        this.changeSelect(temp,this.magjorCategoryList.majorCategory[temp])
        this.$bus.$emit("activeTabToCenter", document.getElementById('label' + temp));
      }
    }
  }
}
回复
1个回答
avatar
test
2024-07-16

建议看一下 阮一峰老师的 Class 的基本语法 - ECMAScript 6入门

类的属性和方法并不是你这样写的。


如果你有想要用混入可以这样写

// myMixin.js
export const myMixin = {
  data() {
    return {
    }
  },
  computed: {
  },
  created() {
  },
  methods:{
  }
}

然后再在你需要引入混入的组件里面这样引入就行了

<template>
    ...
</template>
<script>
import { myMixin } from "@/mixins/myMixin";
export default {
  mixins: [myMixin],
  ...
}
</script>

同时 Vue 的文档也可以好好看看的,特别是在你需要用到的时候。关于混入的部分在这里 👉 混入 — Vue.js

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容