vue写了个全局方法,但是不起作用?

assets/js下我新建了一个global.js文件:

import Vue from "vue";
import Router from "vue-router";

export default({
    //后退
    back:function(){
        this.$router.go(-1);
    },
})

main.js:

//全局方法
import global from './assets/js/global.js'
Vue.prototype.common = global

xx.vue:

<van-icon slot="right" @click="goto()">预览</van-icon>

methods:{
    //跳转
    goto:function(){
        this.common.back();
    }
}

我是不是哪里弄错了,全局方法不起作用?

阅读 3.8k
4 个回答

解决了

//后退
back:function(){
    window.history.go(-1);
},

this是运行时决定的 你的back里面的thiscommon

back:function(){
    this.$router.go(-1);
}

this改成 Vue试试

我理解错了....

这个应该是this的问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题