javascript - 在 Vue 组件中,挂载:{this.init()},引发错误:Unexpected token, expected {
问题描述
在结合 Vue 和 echarts 时,我在 src/components/Chart.vue 中这样写:
<script>
export default {
name: 'Charts',
data () {
return {
chart: ''
}
},
computed: {
style () {
return {
height: this.height,
width: this.width
}
}
},
mounted: {this.init()},
methods: {
init () {
this.chart = this.$echarts.init(document.getElementById(this.id))
this.charts.setOption(this.option)
}
}
}
</script>
在运行时npm run dev
,它提出了这个:
ERROR Failed to compile with 1 errors 22:51:02
error in ./src/components/Chart.vue
Syntax Error: this is a reserved word (57:12)
55 | }
56 | },
> 57 | mounted: {this.init()},
| ^
58 | methods: {
59 | init () {
60 | this.chart = this.$echarts.init(document.getElementById(this.id))
@ ./src/components/Chart.vue 4:0-105 5:0-118
@ ./src/router/index.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
我google了很多,但没有找到答案。如果有人能启发我,我将不胜感激。
解决方案
mounted
应该引用一个函数文字,所以试试这个
mounted: function() { this.init() }
推荐阅读
- pdf - sap adobe livecycle Designer pdf 导入崩溃
- angular - 在生产模式下在Angular6中自定义样式后引导加载
- coldfusion - ColdFusion 为 0 时不会显示查询结果
- c# - .NET DotRas vpn 连接服务器地址解析
- c - 关于如何在C中的一行中获取不同类型的数据的问题
- c++ - 为什么我在使用 C++ 友元函数时会出错?
- bash - 在 shebang 中使用 -l -i 参数失败的 Bash 脚本
- java - 单元格编辑后 JTree 不刷新
- mysql - mysql 中的 FOR EACH ROW 命令
- javascript - 需要帮助以使事件处理程序在 react js 中正常工作