How to use a public method in a .vue file? Currently, I have tried not to call it directly through the method name
For example:
function getId(selector) {
return document.getElementById(selector);
}
I have two questions:
1. Where should the method be written? Should it be written in main.js or create another file common.js and import it into main.js?
2. How do I call it in the Login.vue file? Direct getId() will not work, and an error will be reported getId is not defined
There are generally several loading methods, depending on the structure of the plug-in package, but basically the loading code is added in
main.js
orApp.vue
.If the plug-in package is a pure JS or CSS file and exposes global plug-in variables, you only need to
import 'xxxxxx'
, wherexxxxxx
is the path to JS or CSS. Such plugins include jQuery and Bootstrap.However, it should be noted that the jQuery JS file cannot be used directly after being loaded through
import '....'
. You need to add the following configuration to the packaging script configurationwebpack.base.conf.js
(part of the configuration is omitted):Also pay attention to adding
var webpack = require('webpack')
to the header of this configuration to prevent errorswebpack
is not defined.If the plug-in has been modularized and exposed objects or methods, but cannot be used across modules (such as the
bootbox
plug-in installed into the project throughnpm
), I add it to the frameworkApp.vue
import bootbox from 'bootbox/bootbox.js'
, then add a linebootbox: bootbox
indata
ofApp.vue
, and call the bootbox plugin throughthis.$root.bootbox
in other Vue pages .There is also a plug-in that cannot be loaded through the
import
method. It needs to be loaded usingvar xxx = require('xxx')
. This kind of plug-in is generally used as a plug-in for the Vue framework, such asVue-filter
. This is usually done by addingvar vueFilter = require('vue-filter')
tomain.js
, and then usingVue.use(vueFilter)
to registervue-filter
in the Vue application.According to your description, there is no difference between writing another file in this method and writing it directly in
main.js
. It is recommended to write this method inApp.vue
, then add it tomethods
, and usethis.$root.getId()
where it needs to be called (see the third case above)Just enter
import
directly inmain.js
.It is recommended to learn ES6 modules, write your common methods as modules, and call them in the modules that need to be called. You can also write them in main.js:
Then call
window.util.xxx
globally.this.url.commonUrl
`Written in another file, as a tool class, it can be imported anywhere else, not limited to
.vue
files.(util.js)
(login.vue)
1. Create a new common.js and write it as follows:
(function(win, undefined) {
})(window);
2.Introduce main.js
import "./common.js"
3. Can be called in any .vue file
<script>
export default{
}
</script>
Just like @rehapun's answer, the way of doing it is very good. I usually use it this way. In addition, I would like to add that you don't have to worry about repeated references causing the packaging volume to increase. You can use CommonsChunkPlugin to extract a public vendor when packaging in webpack. .js.
First quote the js file you defined
import 'location of the file'
,