错误:在vue中使用typescript的时候报错“Unexpected token. A constructor, method, accessor or property was expected
demo<script lang="ts">import { Component, Vue } from 'vue-property-decorator'import HelloWorld from '@/components/HelloWorld.vue' // @ is an alias to /src@Component({components: {He...
·
demo
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import HelloWorld from '@/components/HelloWorld.vue' // @ is an alias to /src
@Component({
components: {
HelloWorld
},
props: ['name'],
computed: {},
watch: {},
mounted () {},
created () {}
})
export default class Home extends Vue {
// data
a:number = 1;
b: string = '我是字符串';
c: boolean = true;
d:object = {
a: '你好',
b: '77'
}
// methods
function myClick () :void {
alert('89089')
}
}
</script>
原因
在typescript中定义一个类的时候,不能使用let
const
var
来声明成员变量;在其内部,也禁止使用function
关键字来声明类中的方法,上面报错去掉方法myClick
前面的function
关键字就好了
示例
错误
export class AppComponent {
a: string = "foo";
b: string = "bar";
foo(): void { }
constructor(){
}
}
正确
export class AppComponent {
var a: string = "foo";
let b: string = "bar";
function foo(): void { }
constructor(){
}
}
更多推荐
已为社区贡献31条内容
所有评论(0)