vue injects multiple js globally

Hits: 0

In the project, you will encounter multiple js files that need to be injected globally
. The js above are all interface files. If each page is important, global injection is used.

This method is obviously not cumbersome. Once js has more code, it will be bloated. Therefore, the require.context method that comes with vue is used. The js file to be read must be placed in the [api] file.

//Read the js interface file under the api path 
const apiFiles = require .context( './data/remote/api' , true , /\.js$/)
 const api = apiFiles.keys().reduce( ( api , modulePath ) => {
   // set './app.js' => 'app' 
  const moduleName = modulePath.replace( /^\.\/(.*)\.\w+$/ , '$1' )
   const value = apiFiles(modulePath)
  api[moduleName] = value.default
  return api
}, {})
Vue.prototype.api = api;

use

this.api.filename.method ()

You may also like...

Leave a Reply

Your email address will not be published.