admin管理员组文章数量:1289414
I am setting a vue instance on the window in my main.js
like this:
window.todoEventBus = new Vue()
Inside my ponents I am trying to access this todoEventBus global object like this:
created() {
todoEventBus.$on('pluralise', this.handlePluralise);
},
But I am getting errors saying:
Failed to pile.
./src/ponents/TodoItem.vue
Module Error (from ./node_modules/eslint-loader/index.js):
error: 'todoEventBus' is not defined (no-undef) at src\ponents\TodoItem.vue:57:9:
55 |
56 | created() {
> 57 | todoEventBus.$on('pluralise', this.handlePluralise);
| ^
58 | },
59 |
60 | methods: {
1 error found.
However if I console.log todoEventBus I see the vue object.
My package.json file looks like this.
{
"name": "todo-vue",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.3.2",
"vue": "^2.6.10"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^4.0.0",
"@vue/cli-plugin-eslint": "^4.0.0",
"@vue/cli-service": "^4.0.0",
"babel-eslint": "^10.0.3",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"sass": "^1.23.1",
"sass-loader": "^8.0.0",
"vue-template-piler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:remended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
I am setting a vue instance on the window in my main.js
like this:
window.todoEventBus = new Vue()
Inside my ponents I am trying to access this todoEventBus global object like this:
created() {
todoEventBus.$on('pluralise', this.handlePluralise);
},
But I am getting errors saying:
Failed to pile.
./src/ponents/TodoItem.vue
Module Error (from ./node_modules/eslint-loader/index.js):
error: 'todoEventBus' is not defined (no-undef) at src\ponents\TodoItem.vue:57:9:
55 |
56 | created() {
> 57 | todoEventBus.$on('pluralise', this.handlePluralise);
| ^
58 | },
59 |
60 | methods: {
1 error found.
However if I console.log todoEventBus I see the vue object.
My package.json file looks like this.
{
"name": "todo-vue",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.3.2",
"vue": "^2.6.10"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^4.0.0",
"@vue/cli-plugin-eslint": "^4.0.0",
"@vue/cli-service": "^4.0.0",
"babel-eslint": "^10.0.3",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"sass": "^1.23.1",
"sass-loader": "^8.0.0",
"vue-template-piler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:remended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
Share
Improve this question
asked Oct 27, 2019 at 12:27
Jethro HazelhurstJethro Hazelhurst
3,2857 gold badges42 silver badges85 bronze badges
1
-
1
use
window.todoEventBus
or add thetodoEventBus
in the known globals of eslint – Alex Michailidis Commented Oct 27, 2019 at 12:29
2 Answers
Reset to default 12This error es from the rule no-undef.
Eslint will throw this error when a variable is not defined in scope and it's not a known global (like Promise
, document
, etc...).
You can declare your variable as a global by putting a ment in the file you want to use it like this:
/* global todoEventBus */
or you could declare it as a global in your eslint config
"eslintConfig": {
"globals": {
"todoEventBus": "readable"
}
}
Amending Alex's answer, you could also narrow down the rules to Vue SFC specifically:
// .eslintrc.js
module.exports = {
...
overrides: [
{
files: "*.vue",
globals: {
todoEventBus: "readable",
},
},
],
}
本文标签: javascriptVuenpm run serveESLint global variable is not defined within a componentStack Overflow
版权声明:本文标题:javascript - Vue | npm run serve | ESLint global variable is not defined within a component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741463128a2380154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论