[英]error - Unexpected token in Vue component
我剛開始使用 Vue,並且在一個簡單的組件上遇到了一個頑固的錯誤。 首先,這是錯誤。
Syntax Error: SyntaxError: C:...src\components\Stats.vue: Unexpected token (20:2)
第 20 行:
18 | newLink: '',
19 | },
> 20 | },
| ^
21 | computed: {
22 | ...mapGetters([
23 | 'countLinks',
這是代碼:
<script>
import { mapGetters, mapMutations } from 'vuex'
export default {
name: 'Stats',
data() {
return {
newLink: '',
},
},
computed: {
...mapGetters([
'countLinks',
]),
},
methods: {
...mapMutations([
'ADD_LINK',
]),
addLink: function () {
this.ADD_LINK(this.newLink)
},
},
};
</script>
如您所見,錯誤引用了computed
之前的第 20 行。 但是大括號和逗號都是必需的,所以我不確定可以做些什么來修復它。 似乎沒有任何地方可以像錯誤所暗示的那樣放置分號。
我還嘗試盡可能添加逗號,因為代碼之前抱怨缺少逗號。 然而,這也沒有解決問題。 我四處尋找解決方案,但沒有找到任何可行的方法。
只是為了更新,即使嘗試使用箭頭功能,此錯誤仍然存在:
data: () => ({
newLink: '',
}),
像這樣使用function
關鍵字:
data: function () {
return {
newLink: '',
}
}
或者省略function
並僅使用data
作為函數本身,如下所示:
data() {
return {
newLink: '',
}
},
這些選項中的每一個都返回unexpected token
或semicolon
錯誤。 我幾乎嘗試了所有我能找到的解決方案,搜索了網絡,閱讀了文檔,但沒有找到任何可行的方法。
這是您的代碼及其問題
import { mapGetters, mapMutations } from 'vuex'
export default {
name: 'Stats',
data() {
return {
newLink: '', // 👈 unnecessary comma, syntactically ok
}, // 👈 unnecessary comma, causes an error
},
computed: {
...mapGetters([ // 👈 no need for `...` here
'countLinks', // 👈 unnecessary comma, syntactically ok
]), // 👈 unnecessary comma, syntactically ok
},
methods: {
...mapMutations([
'ADD_LINK', // 👈 unnecessary comma, syntactically ok
]),
addLink: function () {
this.ADD_LINK(this.newLink)
}, // 👈 unnecessary comma, syntactically ok
}, // 👈 unnecessary comma, syntactically ok
};
一個主要問題是這個
data() {
return {
newLink: '',
}, // 👈 this one right here
}
return
試圖評估逗號運算符表達式並返回最后一個操作數。 問題是,沒有最后一個操作數。
這是您的代碼,已修復錯誤並刪除了所有不必要的元素
import { mapGetters, mapMutations } from 'vuex'
export default {
name: 'Stats',
data () {
return {
newLink: ""
}
},
computed: mapGetters(["countLinks"]),
methods: {
...mapMutations(["ADD_LINK"]),
addLink () {
this.ADD_LINK(this.newLink)
}
}
}
您的data
函數也可以寫成
data: () => ({ newLink: "" }),
但這真的取決於個人喜好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.