While working on my vue/vuetify project and attempting to implement optional chaining, I consistently run into an error:
./src/App.vue?vue&type=script&lang=ts& (./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=script&lang=ts&) 155:24
Module parse failed: Unexpected token (155:24)
File was processed with these loaders:
* ./node_modules/cache-loader/dist/cjs.js
* ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| }
|
> const baz = obj?.foo?.bar?.baz // 42
|
| const safe = obj?.qux?.baz // undefined
I have installed
@babel/plugin-proposal-optional-chaining
by using the command yarn add @babel/plugin-proposal-optional-chaining --dev
In my App.vue file, within the created() function, I have the following code:
const obj = {
foo: {
bar: {
baz: 42,
},
},
}
const baz = obj?.foo?.bar?.baz // 42
const safe = obj?.qux?.baz // undefined
console.log({ baz, safe })
The configuration in my babel.config.js is as follows:
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
],
plugins: ["@babel/plugin-proposal-optional-chaining"],
}
I am running vue version 2.6.9.
It seems like everything should be functioning correctly after this setup, but the only similar issue I could find is referenced here: https://github.com/vuejs/vue-loader/issues/1697, even though I am using Babel and not TypeScript.