Home  >  Article  >  Web Front-end  >  How to call methods in js files in vuejs

How to call methods in js files in vuejs

藏色散人
藏色散人Original
2021-09-25 15:37:369849browse

vuejs calls the method in the js file: 1. Create a new js file under the assets file; 2. Use "export default {...}" to reference it in the component that needs to use the method.

How to call methods in js files in vuejs

The operating environment of this article: Windows7 system, Vue2.9.6 version, DELL G3 computer

How does vuejs call the method in the js file ?

How to reference js files in vue

In many components of vue, axios is used to post data, and each component is written with a The post method is also possible, just copy it, but it always feels a bit inconvenient. Wouldn't it be better to write the axios post into a separate js file and then reference it in the required components?

1. Create a new js file under the assets file

Create a new file named webpost.js

import axios from 'axios'
    //Post方法的封装
    function axiosPost(url,params){
        return new Promise((resolve, reject) => {
                axios({
                url: url,
                method: 'post',
                data: params,
                headers: {
                    'Content-Type':'application/json'
                }
            })
            .then(res=>{
                resolve(res.data);
            });
        });
    }
    export {
        axiosPost
    }

This needs to specifically quote axios, which is the first line. Then you can use it. The last sentence is very important, otherwise you will not be able to call

in other components. 2. Reference