what is this configuration error with Vue.js eslint - vue.js

I am newer on vueJs. I try this basic code in my vue to modify datas of a component in a vue:
<template>
<div>
<h1> {{ message }}
<h2> Hello {{ firstname }} {{ lastname }} de {{ from }} ! </h2>
<label> Firstname : <input type="text" v-model="person.firstname" /> </label>
<label> Lastname : <input type="text" v-model="person.lastname" /> </label>
<label> Message : <input type="text" v-model="message" /> </label>
</div>
</template>
<script>
export default {
data () {
person: {
firstname: 'John',
lastname: 'Doe'
},
message: 'Welcome!'
}
}
</script>
I get this error:
Failed to compile.
./src/components/Hello.vue?vue&type=script&lang=js& (./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/Hello.vue?vue&type=script&lang=js&)
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:\Users\1900780\Documents\Afpa\Ressources\PHP\vueJs1\myproject\src\components\Hello.vue: Unexpected token, expected ";" (16:16)
14 | person: {
15 | firstname: 'John',
> 16 | lastname: 'Doe'
| ^
17 | },
18 | message: 'Welcome!'
19 | }
at Object.raise (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:7013:17)
at Object.unexpected (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:8384:16)
at Object.semicolon (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:8366:40)
at Object.parseExpressionStatement (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:11193:10)
at Object.parseStatementContent (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:10792:19)
at Object.parseStatement (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:10658:17)
at Object.parseLabeledStatement (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:11185:22)
at Object.parseStatementContent (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:10790:19)
at Object.parseStatement (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:10658:17)
at Object.parseBlockOrModuleBlockBody (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:11234:25)
at Object.parseBlockBody (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:11221:10)
at Object.parseBlock (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:11205:10)
at Object.parseStatementContent (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:10734:21)
at Object.parseStatement (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:10658:17)
at Object.parseLabeledStatement (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:11185:22)
at Object.parseStatementContent (C:\Users\...\vueJs1\myproject\node_modules\#babel\parser\lib\index.js:10790:19)
I use #vue/cli 4.1.2 and webpack.
My package.json:
{
"name": "myproject",
"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.4.4",
"vue": "^2.6.10"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^4.1.0",
"#vue/cli-plugin-eslint": "^4.1.0",
"#vue/cli-service": "^4.1.0",
"babel-eslint": "^10.0.3",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"vue-template-compiler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint",
"ecmaVersion": 6
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
My code seems to be correct. I don't understand this error. Is there a config to do with babel parser and where is the config file ?
I added an eccmaversion in the package.json file configuration, but it does not seems to work. Any help would be appreciated.

Houps! i just forgot the return statement:
export default {
data () {
return {
person: {
firstname: 'John',
lastname: 'Doe'
},
message: 'Welcome!'
}
}
}

Related

Vue3 Composition API, how to get data from service?

I'm experimenting with the Composition API with Vue3. But there were some points I couldn't find. The same code did not work in two different projects.
What I want to do in my own project is to take the data through the API and use it according to what is required. In short, do the necessary get/post operations. I got this API from Vue's own example.
This is the first project code, package.json and error message
<template>
<div class="home">
<div v-for="datas in data" :key="datas.description">
{{ datas.description }}
</div>
</div>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import axios from "axios";
import { ref } from "vue";
#Options({
props: {
msg: String,
},
})
export default class HelloWorld extends Vue {
setup() {
let data = ref([]);
axios
.get("https://api.coindesk.com/v1/bpi/currentprice.json")
.then((res) => {
data.value = res.data.bpi;
});
}
}
</script>
{
"name": "api-project",
"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.21.1",
"core-js": "^3.6.5",
"vue": "^3.0.0",
"vue-class-component": "^8.0.0-0",
"vue-router": "^4.0.0-0"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-router": "~4.5.0",
"#vue/cli-plugin-typescript": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.0.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
"typescript": "~4.1.5"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
Vue warn
This is the second project code, package.json, and data
<template>
<div v-for="datas in data" :key="datas.description">
{{ datas.description }}
</div>
</template>
<script lang="ts">
import axios from "axios";
import { ref } from "vue";
export default {
name: "HelloWorld",
setup() {
let data = ref([]);
axios.get("https://api.coindesk.com/v1/bpi/currentprice.json").then((res) => {
data.value = res.data.bpi;
});
return {
data,
};
},
}
</script>
{
"name": "test-api",
"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.21.1",
"core-js": "^3.6.5",
"vue": "^3.0.0"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
"typescript": "~4.1.5"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
my data
Could there be an error in my Composition API usage? I've heard that in some videos, "then" is not used for the Composition API. But that's the only way I was able to pull the data from the API.
If my solution is wrong, what method should it be, I'm new at Vuejs can you help?
You need to return the variables from the setup function so that they can be accessed from within the template.
If setup returns an object, the properties on the object can be
accessed in the component's template, as well as the properties of the
props passed into setup:
setup() {
let data = ref([]);
axios
.get("https://api.coindesk.com/v1/bpi/currentprice.json")
.then((res) => {
data.value = res.data.bpi;
});
// return the data as an object
return {
data
}
}
Read more about this in the official vue doc
You can create api dir inside src folder and then inside api dir create a file api.ts and put this code
export async function callApi(endpoint :string, method :string){
return await fetch(endpoint,{
method:method,
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
}).then(async response => {
const resData = await response.json()
if (!response.ok) {
// do something to determine request is not okay
resData.isSuccess = false
}
return resData
}).catch(error => {
console.log("callApi in api.ts err")
console.log(error)
throw error
})
}
Go to you component and use this code
<template>
<div v-for="(item,i) in data.records" v-bind:key="i">
{{ item.chartName}}
</div>
</template>
<script>
import {onMounted,reactive} from "vue"
import {callApi} from "#/api/api"
export default{
name:'MyComponent',
setup() {
const data = reactive({
records: [],
})
onMounted( async() =>{
getRecords()
})
const getRecords = async() => {
let resData = await callApi('https://api.coindesk.com/v1/bpi/currentprice.json', 'GET')
data.records = resData
}
return {
data,
}
}
}
</script>
enter code here

worker-loader doesn't work properly after restart

I'm trying to use web with vue-cli project.
My worker is in file user.worker.js. When I run vue-cli-service serve for first time it works, but after restart webpack tries to load user.worker.worker.js and cant because such file doesn't exist. If I rename or move the file it works for first time but after restart fails again.
App.vue
<template>
<div id="app"></div>
</template>
<script>
import Worker from "#/user.worker";
export default {
name: "App",
created() {
const inst = new Worker();
inst.postMessage({
path: `some data`,
});
},
};
</script>
user.worker.js
onmessage = async ({ path }) => {
postMessage({ path });
};
vue.config.js
module.exports = {
chainWebpack: (config) => {
config.module
.rule("worker")
.test(/\.worker\.js$/)
.use("worker-loader")
.loader("worker-loader")
.end();
},
};
package.json
"name": "worker-project",
"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.6.5",
"vue": "^2.6.11",
"worker-loader": "^3.0.3"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^4.5.0",
"#vue/cli-plugin-eslint": "^4.5.0",
"#vue/cli-service": "^4.5.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}

Why aren't my browsers console logging with WebSockets in Vue?

I hope you can help.
I'm new to WebSockets, I've gotten a simple app running locally and now I wanted to try setting up WS with Vue but am not getting the expected behaviour. I've tried to make code as concise as possible.
I followed this tutorial: https://tutorialedge.net/javascript/vuejs/vuejs-websocket-tutorial/
Goal:
I'm trying to establish a simple WebSockets connection with Vue.
With the code below, I'm running npm run serve(see package.json file), to run my application on http://localhost:8080/.
The problem:
WebSockets connects as I expect it to but when I fire sendMessage with the button, both browsers aren't console logging, which is what I'm expecting.
I've tried changing the WS connection to this.connection = new WebSocket("ws://localhost:8080") but no luck, it never connects and remains in the connecting phase.
I'm receiving no errors in the console.
Can you please help me figure out what I'm doing wrong?
Your help is much appreciated.
Thank you
Moe
The code below is my root Vue.appfile.
<template>
<div id="app">
<button v-on:click="sendMessage">Send Message</button>
</div>
</template>
<script>
export default {
name: 'App',
components: {
testComponent
},
data: function () {
return {
connection: null
}
},
methods: {
sendMessage: function (message) {
this.connection.send(message)
}
},
created: function () {
console.log("Starting Connection to WebSockets Server.")
this.connection = new WebSocket("wss://echo.websocket.org")
this.connection.onopen = function (event) {
console.log(event)
console.log("Successfully connected to the WebSocket Server.")
}
this.connection.onmessage = function (event) {
console.log(event)
}
}
}
</script>
This is my package.json file.
{
"name": "vuejs-websocket-tutorial",
"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.6.5",
"vue": "^2.6.11"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.4.0",
"#vue/cli-plugin-eslint": "~4.4.0",
"#vue/cli-service": "~4.4.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"sass-loader": "^8.0.2",
"vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}

Why is lodash not working when I import it in Vue.js

I created a fresh new install of vue.js using "vue create todo --default" command. After that I installed lodash too with this command "npm i --save lodash". I can see it in my package.json on the "dependencies" object. The problem is that when I import it on my main.js and use the lodash functions, it is showing the error "_ is not defined". So I tried importing it inside the App.vue. The error "_ is not defined" was removed but it is not working.
Here are the code inside the App.vue, main.js, and package.json
main.js
import Vue from 'vue'
import App from './App.vue'
import "bootstrap/dist/css/bootstrap.min.css";
import "jquery/dist/jquery";
import "bootstrap/dist/js/bootstrap.min";
import _ from "lodash";
Vue.prototype._ = _;
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
App.vue
<template>
<div id="app">
<h4 class="bg-primary text-white text-center p-2">
{{name}}'s' To Do List
</h4>
<div class="container-fluid p-4">
<div class="row">
<div class="col font-weight-bold">Task</div>
<div class="col-2 font-weight-bold">Done</div>
</div>
<div class="row" v-for="t in completedtask" v-bind:key="t.action">
<div class="col">{{t.action}}</div>
<div class="col-2">
<input type="checkbox" v-model="t.done" class="form-check-input">
{{t.done}}
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return{
name: "Welly",
tasks: [{
action: "Buy Flowers",
done: false
},
{
action: "Get Shoes",
done: false
},
{
action: "Collect Tickets",
done: true
},
{
action: "Call Joe",
done: false
}
]
};
},
computed: {
hidecompletedtask(){
return _.map(this.tasks,(val)=>{
return !val.done;
});
}
}
}
</script>
<style>
</style>
package.json
{
"name": "todo",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"bootstrap": "^4.4.1",
"core-js": "^3.4.4",
"jquery": "^3.4.1",
"lodash": "^4.17.15",
"popper.js": "^1.16.1",
"vue": "^2.6.10"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^4.1.0",
"#vue/cli-plugin-eslint": "^4.1.0",
"#vue/cli-service": "^4.1.0",
"babel-eslint": "^10.0.3",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"vue-template-compiler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
You'll still need to access the prototype via the this context, like this._.map().
computed: {
hidecompletedtask() {
return this._.map(this.tasks, (val) => {
return !val.done;
});
}
}
Reference: Adding Instance Properties.
Alternatively, you could extend the global window object. Put the following line in your main.js (or some booting file).
window._ = require('lodash');
Somewhere else where you need the library:
computed: {
hidecompletedtask() {
// The underscore (_) character now refers to the `window._ object`
// so you can drop the `this`.
return _.map(this.tasks, (val) => {
return !val.done;
});
}
}
You can also use vue-lodash package -- Follow these steps:
npm install --save vue-lodash
in main.js -- import VueLodash from 'vue-lodash'
in main.js after import -- Vue.use(VueLodash)
Usage:
Vue._.random(20);
this._.random(20);
-------- OR ------------
In your main.js add this line of code:
window._ = require('lodash');
That way it will work without Vue or this:
Just do -- _.map()
You can import lodash in your main.js file by javascript window object like this:
window._ = require('lodash');
Then use it anywhere in your projet like this:
var original = [
{ label: 'private', value: 'private#johndoe.com' },
{ label: 'work', value: 'work#johndoe.com' }
];
var update = [
{ label: 'private', value: 'me#johndoe.com' },
{ label: 'school', value: 'schol#johndoe.com' }
];
var result = _.unionBy(update, original);
var sortedresult = _map(_.sortBy(result, 'label'));
console.log(sortedresult);
I just use lodash unionBy and sortBy method for example.

Why does Vue convert my single quotes to double quotes in my background-image url?

Every post I've found on this topic referts to using Vetur or Prettier. I'm using neither. This is a basic Vue CLI install. The only thing I've added is sass-loader. I've added my package.json to the bottom of the post to confirm what I'm actually using.
<template>
<div class="hero-section" :style="{ backgroundImage: 'url(' + currentBgImage + ')' }">
<i class="fa fa-chevron-left" #click="previousImage()"></i>
<i class="fa fa-chevron-right" #click="nextImage()"></i>
</div>
</template>
<script>
export default {
name: 'HeroSlider',
props: {
imageArray: Array
},
data() {
return {
selectedImage: 0,
};
},
computed: {
currentBgImage() {
let array = this.imageArray;
let result = array[this.selectedImage].image;
return result;
}
},
The result is:
<div class="hero-section" style="background-image:url("/src/assets/image_1.jpg");">
<i class="fa fa-chevron-left"></i>
<i class="fa fa-chevron-right"></i>
</div>
I've also tried including the quotes in the computed property's value and in the html but the result was the same:
:style="{ backgroundImage: 'url(\'' + currentBgImage + '\')'
Now, something curious happened as I copied this HTML from my inspection window. When I pasted it here it initially pasted like this with " instead of ". Could that be an indicator of what's going wrong?
<div class="hero-section" style="background-image: url("/src/assets/image_1.jpg");">
<i class="fa fa-chevron-left"></i>
<i class="fa fa-chevron-right"></i>
</div>
package.json
{
"name": "heroslider",
"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",
"node-sass": "^4.13.0",
"sass-loader": "^8.0.0",
"vue-template-compiler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {
"no-console": "off"
},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
Why does Vue convert my single quotes to double quotes in my background-image url?
It doesn't.
See the following example, which isn't using any libraries at all:
const el = document.getElementById('div')
el.style.backgroundImage = 'url(https://vuejs.org/images/logo.png)'
console.log(el.style.backgroundImage)
console.log(el.outerHTML)
#div {
height: 100px;
width: 100px;
}
<div id="div"></div>
Notice that the console logging has " characters around the URL even though they didn't appear in the original value. Those are encoded to " within the HTML, which is correct.
The browser is normalizing the value. It shouldn't make any difference to the URL that is requested from the server. If your image isn't loading the quotes are not the problem. That should be evident by checking the URL being used to load the image in the Network tab of your browser's developer tools.