当前位置: 首页 > 工具软件 > code1024 > 使用案例 >

【vue】页面加载时自动获取code并发送请求

姚智
2023-12-01

在vue页面中,页面加载时

1.自动执行获取url地址以及参数

2.发送请求给后端接口

3.自动跳转到别的页面用路由跳转

所有代码如下

export default {


 

    data() {

        return {

            form: {

                code: "",

                state: "",

                href: "",

            },

        };

    },

    mounted: function(){

        this.getcode(); //页面加载时自动执行

    },

    methods: {

       

        getcode() {

            var url = '地址或者用window.location.href';

            var tem = url;

            if (tem.length > 0) {

                this.href = tem;

            }

            let getqyinfo = url.split('?')[1]

            let getqys = getqyinfo.split('&')

            let getcode = getqys[0].split('=')[1]

            let getstate = getqys[1].split('=')[1]

            this.code = getcode;

            this.state = getstate;

           

            console.log('code:', getcode)

            console.log('state:', getstate)

            // console.log(data)

            this.$post(this.接口, this.form).then(

                (data) => {

                    this.getcode = data.code;

                    this.getstate = data.state;

                }

            )

            this.$router.replace('/index');//路由跳转

        },

    }






 

}

 类似资料: