idb-js icon indicating copy to clipboard operation
idb-js copied to clipboard

关于在vue中使用场景

Open Emberla opened this issue 3 years ago • 1 comments

简单场景:调用登录接口,将token存入indexDB,存储成功后,在封装的idb的success回调中是否能支持$router跳转,之前我使用过为封装的indexDB,在类似场景中,存储成功后不能跳转业务场景。

Emberla avatar Aug 03 '21 10:08 Emberla

this.$refs.loginForm.validate((valid) => { if (valid) { this.loading = true; this.$store .dispatch("user/login", this.loginForm) .then((res) => { console.log('login res', res); window.localStorage.setItem('username', this.loginForm.username); // let paths = this.redirect == undefined || this.redirect == "/" ? "/index" : this.redirect;

			const request = indexedDB.open('myDatabase', 1);
			request.addEventListener('success', e => {
			    console.log('连接数据库成功');
				const db = e.target.result;
				const tx = db.transaction('Users', 'readwrite');
				const store = tx.objectStore('Users');
				const reqAdd = store.add({
					'userId': 1,
					'userName': '李白',
					'age': 24
				});
				reqAdd.addEventListener('success', e => {
					console.log('保存成功')
                                            // TODO
				})
			});
			
			request.addEventListener('error', e => {
			    console.log('连接数据库失败');
			});
			request.addEventListener('upgradeneeded', e => {
			    const db = e.target.result;
			    const store = db.createObjectStore('Users', {
			        keyPath: 'userId',
			        autoIncrement: false
			    });
			    console.log('创建对象仓库成功');
			});
			
			
			that.loading = false;
			that.$router.push({
				path: paths
			});
			
		})
        .catch((e) => {
			console.log("登录失败", e);
			this.$message({
				message: e,
				type: "error",
				duration: 2000
			});
		this.loading = false;
		this.getCode();
        });
    } else {
      return false;
    }
  });

在todo的地方执行loading变量和路由跳转,是不执行的。

Emberla avatar Aug 03 '21 10:08 Emberla

indexdb和vue并不会互相影响,路由调整不执行可能是你的this并不是指向vue中的router

verybigorange avatar Aug 08 '23 08:08 verybigorange