What does the @ mean inside an import path?
This is done with Webpack resolve.alias
configuration option and isn't specific to Vue.
In Vue Webpack template, Webpack is configured to replace @/
with src
path:
const path = require('path');
...
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
...
'@': path.resolve('src'),
}
},
...
The alias is used as:
import '@/<path inside src folder>';
What does the @ symbol do in javascript imports?
The meaning and structure of the module identifier depends on the module loader or module bundler. The module loader is not part of the ECMAScript spec. From a JavaScript language perspective, the module identifier is completely opaque. So it really depends on which module loader/bundler you are using.
You most likely have something like babel-plugin-root-import in your webpack/babel config.
Basically it means from the root of the project.. it avoids having to write things like import Component from '../../../../components/component'
Edit: One reason it exists is because import Component from 'components/component'
doesn't do that but instead search in the node_modules
folder
How to import vue components?
You have 2 way to import component the first local if this components will be used in only one or 2 page you can import it like this
<script>
import ButtonCounter from './ButtonCounter.vue'
export default { components: { ButtonCounter } }
</script>
If this components will be used by too much page you can import it globaly like this in the main.js
import MyComponent from './App.vue'
app.component('ButtonCounter', ButtonCounter)
vue js import from node_modules
Importing using only the package name will always look in the node_modules
folder, regardless of whether the node_modules
is in the current directory, the parent directory, or the global directory. Unless if the path is preceded by ./
, ../
, etc.
Related Topics
How to Create Multidimensional Array
JavaScript - How to Extract Filename from a File Input Control
Console Log the State After Using Usestate Doesn't Return the Current Value
JavaScript Get Window X/Y Position for Scroll
Redirect a State to Default Substate with Ui-Router in Angularjs
Variable Scope in D3 JavaScript
Multiple Left-Hand Assignment with JavaScript
Are Es6 Classes Just Syntactic Sugar for the Prototypal Pattern in JavaScript
Difference Between JSON.Stringify and JSON.Parse
New Es6 Syntax for Importing Commonjs/Amd Modules I.E. 'Import Foo = Require('Foo')'
Failed to Execute 'Postmessage' on 'Domwindow': Https://Www.Youtube.Com !== Http://Localhost:9000
Why Does !New Boolean(False) Equals False in JavaScript
How to Count Certain Elements in Array
Using Jquery to Replace One Tag with Another
Es6 Array Destructuring Weirdness
What Is the Order of Execution in JavaScript Promises
How to Convert Dd/Mm/Yyyy String into JavaScript Date Object