如何在 vue 脚本中修复“eslint(no-unused-vars)”
Posted
技术标签:
【中文标题】如何在 vue 脚本中修复“eslint(no-unused-vars)”【英文标题】:How to fix "eslint(no-unused-vars)" in vue script 【发布时间】:2020-02-17 01:14:15 【问题描述】:我正在尝试在 Vuejs 应用程序中运行服务器,但我遇到了 eslint 消息的问题,感谢您帮助修复它们
这是我的 Json 文件:
"name": "blog-frontend",
"version": "0.1.0",
"private": true,
"scripts":
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
,
"dependencies":
"axios": "^0.19.0",
"core-js": "^3.3.2",
"vue": "^2.6.10",
"vue-router": "^3.0.6"
,
"devDependencies":
"@vue/cli-plugin-babel": "^4.0.0",
"@vue/cli-plugin-eslint": "^4.0.0",
"@vue/cli-plugin-router": "^4.0.0",
"@vue/cli-service": "^4.0.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"vue-template-compiler": "^2.6.10"
这是控制台上的消息:
ERROR Failed to compile with 1 errors 5:11:39 PM
error in ./src/views/Home.vue
Module Error (from ./node_modules/eslint-loader/index.js):
error: 'server' is defined but never used (no-unused-vars) at src\views\Home.vue:40:9:
38 | <script>
39 | // @ is an alias to /src
> 40 | import server from "@/utils/helper";
| ^
41 | import axios from "axios";
42 |
43 | export default
error: 'id' is defined but never used (no-unused-vars) at src\views\Home.vue:58:16:
56 | .then(data => (this.posts = data.data));
57 | ,
> 58 | deletePost(id)
| ^
59 | axios.delete('$sever.baseURL/blog/delete?postID=$id').then(data =>
60 | console.log(data);
61 | window.location.reload();
2 errors found.
@ ./src/router.js 3:0-41 19:15-28
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://192.168.1.152:8080/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
Home.vue
<script>
// @ is an alias to /src
import server from "@/utils/helper";
import axios from "axios";
export default
data()
return
posts: []
;
,
created()
this.fetchPosts();
,
methosd:
fetchPosts()
axios
.get('$server.baseURL/blog/posts')
.then(data => (this.posts = data.data));
,
deletePost(id)
axios.delete('$sever.baseURL/blog/delete?postID=$id').then(data =>
console.log(data);
window.location.reload();
);
;
</script>
helper.js
export const server =
baseURL: 'http:localhost:3000'
main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
new Vue(
router,
render: h => h(App)
).$mount('#app')
我希望有足够的信息,您可以帮助我修复它们
【问题讨论】:
methosd
应该是 methods
而在 deletePost
中,你有 sever
而不是 server
另外,您在template literals 中使用单引号,但它们应该是反引号,即`$server.baseURL/blog/posts`
【参考方案1】:
使用字符串模板正确构建您的网址,如下所示;
axios.delete(`$server.baseURL/blog/delete?postID=$id`).then(data =>
然后将使用server
和id
并修复错误。
【讨论】:
以上是关于如何在 vue 脚本中修复“eslint(no-unused-vars)”的主要内容,如果未能解决你的问题,请参考以下文章
如何修复或抑制误报“您可能在组件渲染函数中有无限更新循环”Vue 警告
在 Vue 3 中使用 Vuex 4 模块和 TypeScript,以及如何修复循环依赖 linting 错误?
如何修复 Spring Boot + Vue 应用程序中损坏的 CORS?
如何修复“未捕获的 ReferenceError:Vue 未在 vuetemplatetest.2.html:19 中定义”