vue 页面路由跳转-离开此页面提示是否保存后再离开

此功能是点击导航跳转前提示是否保存后在离开,不是关闭浏览器前提示

1.首先定义一个变量

代码如下(示例):

data() {
    return {
      contentHasSave: false //记录是否点击提交了
    };
  },

2.跳转路由前做判断

代码如下(示例):

  data(){
	return{
  		contentHasSave: false //记录是否点击提交了
    };
  },
  beforeRouteLeave(to, from , next){
      let _this = this
      if (this.contentHasSave == false) {
          next(false)
          this.$confirm('离开此页面将自动交卷','提示',{
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(()=>{
            //   选择确定
            next()
            _this.putLearningObj() //提交考试结果函数
          }).catch(()=>{

          })
      }
  },

总结

提示:如何在关闭浏览器时也做出同样的提示呢,欢迎留言~

Vue中,可以使用Vue Router来进行页面路由跳转。下面是一个简单的示例: 1. 首先,确保已经安装了Vue Router。可以通过以下命令进行安装: ``` npm install vue-router ``` 2. 在你的Vue项目中,创建一个`router.js`文件,用于配置路由。在该文件中,引入VueVue Router,并创建一个路由实例。示例代码如下: ```js import Vue from 'vue'; import VueRouter from 'vue-router'; // 引入你的组件 import Home from './components/Home.vue'; import About from './components/About.vue'; import Contact from './components/Contact.vue'; Vue.use(VueRouter); const routes = [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ]; const router = new VueRouter({ routes }); export default router; ``` 3. 在你的Vue实例中,使用该路由实例。示例代码如下: ```js import Vue from 'vue'; import App from './App.vue'; import router from './router'; new Vue({ render: h => h(App), router }).$mount('#app'); ``` 4. 在你的组件中,可以使用`<router-link>`标签进行路由跳转。示例代码如下: ```html <template> <div> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> <router-link to="/contact">Contact</router-link> </div> </template> ``` 5. 可以通过`<router-view>`标签来显示当前路由对应的组件。示例代码如下: ```html <template> <div> <router-view></router-view> </div> </template> ``` 这样,你就可以通过点击`<router-link>`标签来实现页面路由跳转了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值